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 | |
Willy Tarreau | 714f345 | 2021-05-09 06:47:26 +0200 | [diff] [blame] | 491 | This example code is used in HAProxy configuration like this: |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 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 | |
Willy Tarreau | 714f345 | 2021-05-09 06:47:26 +0200 | [diff] [blame] | 514 | This example code is used in HAProxy configuration like this: |
Thierry FOURNIER | c5d11c6 | 2018-02-12 14:46:54 +0100 | [diff] [blame] | 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 | |
Willy Tarreau | 714f345 | 2021-05-09 06:47:26 +0200 | [diff] [blame] | 638 | This example code is used in HAProxy configuration like this: |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 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 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1281 | .. image:: _static/channel.png |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1282 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1283 | .. warning:: |
| 1284 | It is not possible to read from the response in request action, and it is |
| 1285 | not possible to read for the request channel in response action. |
Christopher Faulet | 0953039 | 2021-06-14 11:43:18 +0200 | [diff] [blame] | 1286 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1287 | .. warning:: |
| 1288 | It is forbidden to alter the Channels buffer from HTTP contexts. So only |
| 1289 | :js:func:`Channel.input`, :js:func:`Channel.output`, |
| 1290 | :js:func:`Channel.may_recv`, :js:func:`Channel.is_full` and |
| 1291 | :js:func:`Channel.is_resp` can be called from an HTTP conetext. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1292 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1293 | .. note:: |
| 1294 | Channel object may only be manipulated in the context of a registered |
| 1295 | action, sample-fetch or converter. However, only actions are allowed to |
| 1296 | yield. When it is said one of the following functions may yield, it is only |
| 1297 | true in the context of an action. |
| 1298 | |
| 1299 | .. js:function:: Channel.append(channel, string) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1300 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1301 | This function copies the string **string** at the end of incoming data of the |
| 1302 | channel buffer. The function returns the copied length on success or -1 if |
| 1303 | data cannot be copied. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1304 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1305 | Same that :js:func:`Channel.insert(channel, string, channel:input())`. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1306 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1307 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1308 | :param string string: The data to copied into incoming data. |
| 1309 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1310 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1311 | .. js:function:: Channel.data(channel [, offset [, length]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1312 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1313 | This function returns **length** bytes of incoming data from the channel |
| 1314 | buffer, starting at the offset **offset**. The data are not removed from the |
| 1315 | buffer. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1316 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1317 | By default, if no length is provided, all incoming data found, starting at the |
| 1318 | given offset, are returned. If **length** is set to -1, the function tries to |
| 1319 | retrieve a maximum of data and yields if necessary. It also waits for more |
| 1320 | data if the requested length exceeds the available amount of incoming data. Do |
| 1321 | not providing an offset is the same that setting it to 0. A positive offset is |
| 1322 | relative to the begining of incoming data of the channel buffer while negative |
| 1323 | offset is relative to their end. |
| 1324 | |
| 1325 | If there is no incoming data and the channel can't receive more data, a 'nil' |
| 1326 | value is returned. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1327 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1328 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1329 | :param integer offset: *optional* The offset in incoming data to start to get |
| 1330 | data. 0 by default. May be negative to be relative to |
| 1331 | the end of incoming data. |
| 1332 | :param integer length: *optional* The expected length of data to retrieve. All |
| 1333 | incoming data by default. May be set to -1 to get a |
| 1334 | maximum of data. |
| 1335 | :returns: a string containing the data found or nil. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1336 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1337 | .. js:function:: Channel.forward(channel, length) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1338 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1339 | This function forwards **length** bytes of data from the channel buffer. If |
| 1340 | the requested length exceeds the available amount of incoming data, the |
| 1341 | function yields, waiting for more data to forward. It returns the amount of |
| 1342 | data forwarded. |
| 1343 | |
| 1344 | :param class_channel channel: The manipulated Channel. |
| 1345 | :param integer int: The amount of data to forward. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1346 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1347 | .. js:function:: Channel.input(channel) |
| 1348 | |
| 1349 | This function returns the length of incoming data of the channel buffer. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1350 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1351 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1352 | :returns: an integer containing the amount of available bytes. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1353 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1354 | .. js:function:: Channel.insert(channel, string [, offset]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1355 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1356 | This function copies the string **string** at the offset **offset** in |
| 1357 | incoming data of the channel buffer. The function returns the copied length on |
| 1358 | success or -1 if data cannot be copied. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1359 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1360 | By default, if no offset is provided, the string is copied in front of |
| 1361 | incoming data. A positive offset is relative to the begining of incoming data |
| 1362 | of the channel buffer while negative offset is relative to their end. |
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 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1365 | :param string string: The data to copied into incoming data. |
| 1366 | :param integer offset: *optional* The offset in incomding data where to copied |
| 1367 | data. 0 by default. May be negative to be relative to |
| 1368 | the end of incoming data. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1369 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1370 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1371 | .. js:function:: Channel.is_full(channel) |
| 1372 | |
| 1373 | This function returns true if the channel buffer is full. |
| 1374 | |
| 1375 | :returns: a boolean |
| 1376 | |
| 1377 | .. js:function:: Channel.is_resp(channel) |
| 1378 | |
| 1379 | This function returns true if the channel is the response one. |
| 1380 | |
| 1381 | :returns: a boolean |
| 1382 | |
| 1383 | .. js:function:: Channel.line(channel [, offset [, length]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1384 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1385 | This function parses **length** bytes of incoming data of the channel buffer, |
| 1386 | starting at offset **offset**, and returns the first line found, including the |
| 1387 | '\n'. The data are not removed from the buffer. If no line is found, all data |
| 1388 | are returned. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1389 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1390 | By default, if no length is provided, all incoming data, starting at the given |
| 1391 | offset, are evaluated. If **length** is set to -1, the function tries to |
| 1392 | retrieve a maximum of data and yields if necessary. It also waits for more |
| 1393 | data if the requested length exceeds the available amount of incoming data. Do |
| 1394 | not providing an offset is the same that setting it to 0. A positive offset is |
| 1395 | relative to the begining of incoming data of the channel buffer while negative |
| 1396 | offset is relative to their end. |
| 1397 | |
| 1398 | If there is no incoming data and the channel can't receive more data, a 'nil' |
| 1399 | value is returned. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1400 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1401 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1402 | :param integer offset: *optional* The offset in incomding data to start to |
| 1403 | parse data. 0 by default. May be negative to be |
| 1404 | relative to the end of incoming data. |
| 1405 | :param integer length: *optional* The length of data to parse. All incoming |
| 1406 | data by default. May be set to -1 to get a maximum of |
| 1407 | data. |
| 1408 | :returns: a string containing the line found or nil. |
| 1409 | |
| 1410 | .. js:function:: Channel.may_recv(channel) |
| 1411 | |
| 1412 | This function returns true if the channel may still receive data. |
| 1413 | |
| 1414 | :returns: a boolean |
| 1415 | |
| 1416 | .. js:function:: Channel.output(channel) |
| 1417 | |
| 1418 | This function returns the length of outgoing data of the channel buffer. |
| 1419 | |
| 1420 | :param class_channel channel: The manipulated Channel. |
| 1421 | :returns: an integer containing the amount of available bytes. |
| 1422 | |
| 1423 | .. js:function:: Channel.prepend(channel, string) |
| 1424 | |
| 1425 | This function copies the string **string** in front of incoming data of the |
| 1426 | channel buffer. The function returns the copied length on success or -1 if |
| 1427 | data cannot be copied. |
| 1428 | |
| 1429 | Same that :js:func:`Channel.insert(channel, string, 0)`. |
| 1430 | |
| 1431 | :param class_channel channel: The manipulated Channel. |
| 1432 | :param string string: The data to copied into incoming data. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1433 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1434 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1435 | .. js:function:: Channel.remove(channel [, offset [, length]]) |
| 1436 | |
| 1437 | This function removes **length** bytes of incoming data of the channel buffer, |
| 1438 | starting at offset **offset**. This function returns number of bytes removed |
| 1439 | on success. |
| 1440 | |
| 1441 | By default, if no length is provided, all incoming data, starting at the given |
| 1442 | offset, are removed. Do not providing an offset is the same that setting it |
| 1443 | to 0. A positive offset is relative to the begining of incoming data of the |
| 1444 | channel buffer while negative offset is relative to their end. |
| 1445 | |
| 1446 | :param class_channel channel: The manipulated Channel. |
| 1447 | :param integer offset: *optional* The offset in incomding data where to start |
| 1448 | to remove data. 0 by default. May be negative to |
| 1449 | be relative to the end of incoming data. |
| 1450 | :param integer length: *optional* The length of data to remove. All incoming |
| 1451 | data by default. |
| 1452 | :returns: an integer containing the amount of bytes removed. |
| 1453 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1454 | .. js:function:: Channel.send(channel, string) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1455 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1456 | This function required immediate send of the string **string**. It means the |
| 1457 | string is copied at the begining of incoming data of the channel buffer and |
| 1458 | immediately forwarded. Unless if the connection is close, this function yields |
| 1459 | to copied and forward all the string. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1460 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1461 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1462 | :param string string: The data to send. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1463 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1464 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1465 | .. js:function:: Channel.set(channel, string [, offset [, length]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1466 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1467 | This function replace **length** bytes of incoming data of the channel buffer, |
| 1468 | starting at offset **offset**, by the string **string**. The function returns |
| 1469 | the copied length on success or -1 if data cannot be copied. |
| 1470 | |
| 1471 | By default, if no length is provided, all incoming data, starting at the given |
| 1472 | offset, are replaced. Do not providing an offset is the same that setting it |
| 1473 | to 0. A positive offset is relative to the begining of incoming data of the |
| 1474 | channel buffer while negative offset is relative to their end. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1475 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1476 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1477 | :param string string: The data to copied into incoming data. |
| 1478 | :param integer offset: *optional* The offset in incomding data where to start |
| 1479 | the data replacement. 0 by default. May be negative to |
| 1480 | be relative to the end of incoming data. |
| 1481 | :param integer length: *optional* The length of data to replace. All incoming |
| 1482 | data by default. |
| 1483 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1484 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1485 | .. js:function:: Channel.dup(channel) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1486 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1487 | **DEPRECATED** |
| 1488 | |
| 1489 | This function returns all incoming data found in the channel buffer. The data |
| 1490 | are not remove from the buffer and can be reprocessed later. |
| 1491 | |
| 1492 | If there is no incoming data and the channel can't receive more data, a 'nil' |
| 1493 | value is returned. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1494 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1495 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1496 | :returns: a string containing all data found or nil. |
| 1497 | |
| 1498 | .. warning:: |
| 1499 | This function is deprecated. :js:func:`Channel.data()` must be used |
| 1500 | instead. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1501 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1502 | .. js:function:: Channel.get(channel) |
| 1503 | |
| 1504 | **DEPRECATED** |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1505 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1506 | This function returns all incoming data found in the channel buffer and remove |
| 1507 | them from the buffer. |
| 1508 | |
| 1509 | If there is no incoming data and the channel can't receive more data, a 'nil' |
| 1510 | value is returned. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1511 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1512 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1513 | :returns: a string containing all the data found or nil. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1514 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1515 | .. warning:: |
| 1516 | This function is deprecated. :js:func:`Channel.data()` must be used to |
| 1517 | retrieve data followed by a call to :js:func:`Channel:remove()` to remove |
| 1518 | data. |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 1519 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1520 | .. code-block:: lua |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 1521 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame^] | 1522 | local data = chn:data() |
| 1523 | chn:remove(0, data:len()) |
| 1524 | |
| 1525 | .. |
| 1526 | |
| 1527 | .. js:function:: Channel.getline(channel) |
| 1528 | |
| 1529 | **DEPRECATED** |
| 1530 | |
| 1531 | This function returns the first line found in incoming data of the channel |
| 1532 | buffer, including the '\n'. The returned data are removed from the buffer. If |
| 1533 | no line is found, this function yields to wait for more data, except if the |
| 1534 | channel can't receive more data. In this case all data are returned. |
| 1535 | |
| 1536 | If there is no incoming data and the channel can't receive more data, a 'nil' |
| 1537 | value is returned. |
| 1538 | |
| 1539 | :param class_channel channel: The manipulated Channel. |
| 1540 | :returns: a string containing the line found or nil. |
| 1541 | |
| 1542 | .. warning:: |
| 1543 | This function is depdrecated. :js:func:`Channel.line()` must be used to |
| 1544 | retrieve a line followed by a call to :js:func:`Channel:remove()` to remove |
| 1545 | data. |
| 1546 | |
| 1547 | .. code-block:: lua |
| 1548 | |
| 1549 | local line = chn:line(0, -1) |
| 1550 | chn:remove(0, line:len()) |
| 1551 | |
| 1552 | .. |
| 1553 | |
| 1554 | .. js:function:: Channel.get_in_len(channel) |
| 1555 | |
| 1556 | **DEPDRECATED** |
| 1557 | |
| 1558 | This function returns the length of the input part of the buffer. |
| 1559 | |
| 1560 | :param class_channel channel: The manipulated Channel. |
| 1561 | :returns: an integer containing the amount of available bytes. |
| 1562 | |
| 1563 | .. warning:: |
| 1564 | This function is deprecated. :js:func:`Channel.input()` must be used |
| 1565 | instead. |
| 1566 | |
| 1567 | .. js:function:: Channel.get_out_len(channel) |
| 1568 | |
| 1569 | **DEPDRECATED** |
| 1570 | |
| 1571 | This function returns the length of the output part of the buffer. |
| 1572 | |
| 1573 | :param class_channel channel: The manipulated Channel. |
| 1574 | :returns: an integer containing the amount of available bytes. |
| 1575 | |
| 1576 | .. warning:: |
| 1577 | This function is deprecated. :js:func:`Channel.output()` must be used |
| 1578 | instead. |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1579 | |
| 1580 | .. _http_class: |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1581 | |
| 1582 | HTTP class |
| 1583 | ========== |
| 1584 | |
| 1585 | .. js:class:: HTTP |
| 1586 | |
| 1587 | This class contain all the HTTP manipulation functions. |
| 1588 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 1589 | .. js:function:: HTTP.req_get_headers(http) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1590 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1591 | Returns a table containing all the request headers. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1592 | |
| 1593 | :param class_http http: The related http object. |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1594 | :returns: table of headers. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1595 | :see: :js:func:`HTTP.res_get_headers` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1596 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1597 | This is the form of the returned table: |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1598 | |
| 1599 | .. code-block:: lua |
| 1600 | |
| 1601 | HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>" |
| 1602 | |
| 1603 | local hdr = HTTP:req_get_headers() |
| 1604 | hdr["host"][0] = "www.test.com" |
| 1605 | hdr["accept"][0] = "audio/basic q=1" |
| 1606 | hdr["accept"][1] = "audio/*, q=0.2" |
| 1607 | hdr["accept"][2] = "*/*, q=0.1" |
| 1608 | .. |
| 1609 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 1610 | .. js:function:: HTTP.res_get_headers(http) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1611 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1612 | Returns a table containing all the response headers. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1613 | |
| 1614 | :param class_http http: The related http object. |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1615 | :returns: table of headers. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1616 | :see: :js:func:`HTTP.req_get_headers` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1617 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1618 | This is the form of the returned table: |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1619 | |
| 1620 | .. code-block:: lua |
| 1621 | |
| 1622 | HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>" |
| 1623 | |
| 1624 | local hdr = HTTP:req_get_headers() |
| 1625 | hdr["host"][0] = "www.test.com" |
| 1626 | hdr["accept"][0] = "audio/basic q=1" |
| 1627 | hdr["accept"][1] = "audio/*, q=0.2" |
| 1628 | hdr["accept"][2] = "*.*, q=0.1" |
| 1629 | .. |
| 1630 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1631 | .. js:function:: HTTP.req_add_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1632 | |
| 1633 | Appends an HTTP header field in the request whose name is |
| 1634 | specified in "name" and whose value is defined in "value". |
| 1635 | |
| 1636 | :param class_http http: The related http object. |
| 1637 | :param string name: The header name. |
| 1638 | :param string value: The header value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1639 | :see: :js:func:`HTTP.res_add_header` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1640 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1641 | .. js:function:: HTTP.res_add_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1642 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1643 | Appends an HTTP header field in the response whose name is |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1644 | specified in "name" and whose value is defined in "value". |
| 1645 | |
| 1646 | :param class_http http: The related http object. |
| 1647 | :param string name: The header name. |
| 1648 | :param string value: The header value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1649 | :see: :js:func:`HTTP.req_add_header` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1650 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1651 | .. js:function:: HTTP.req_del_header(http, name) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1652 | |
| 1653 | Removes all HTTP header fields in the request whose name is |
| 1654 | specified in "name". |
| 1655 | |
| 1656 | :param class_http http: The related http object. |
| 1657 | :param string name: The header name. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1658 | :see: :js:func:`HTTP.res_del_header` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1659 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1660 | .. js:function:: HTTP.res_del_header(http, name) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1661 | |
| 1662 | Removes all HTTP header fields in the response whose name is |
| 1663 | specified in "name". |
| 1664 | |
| 1665 | :param class_http http: The related http object. |
| 1666 | :param string name: The header name. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1667 | :see: :js:func:`HTTP.req_del_header` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1668 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1669 | .. js:function:: HTTP.req_set_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1670 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1671 | This variable replace all occurrence of all header "name", by only |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1672 | one containing the "value". |
| 1673 | |
| 1674 | :param class_http http: The related http object. |
| 1675 | :param string name: The header name. |
| 1676 | :param string value: The header value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1677 | :see: :js:func:`HTTP.res_set_header` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1678 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1679 | This function does the same work as the following code: |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1680 | |
| 1681 | .. code-block:: lua |
| 1682 | |
| 1683 | function fcn(txn) |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1684 | TXN.http:req_del_header("header") |
| 1685 | TXN.http:req_add_header("header", "value") |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1686 | end |
| 1687 | .. |
| 1688 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1689 | .. js:function:: HTTP.res_set_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1690 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1691 | This variable replace all occurrence of all header "name", by only |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1692 | one containing the "value". |
| 1693 | |
| 1694 | :param class_http http: The related http object. |
| 1695 | :param string name: The header name. |
| 1696 | :param string value: The header value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1697 | :see: :js:func:`HTTP.req_rep_header()` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1698 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 1699 | .. js:function:: HTTP.req_rep_header(http, name, regex, replace) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1700 | |
| 1701 | Matches the regular expression in all occurrences of header field "name" |
| 1702 | according to "regex", and replaces them with the "replace" argument. The |
| 1703 | replacement value can contain back references like \1, \2, ... This |
| 1704 | function works with the request. |
| 1705 | |
| 1706 | :param class_http http: The related http object. |
| 1707 | :param string name: The header name. |
| 1708 | :param string regex: The match regular expression. |
| 1709 | :param string replace: The replacement value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1710 | :see: :js:func:`HTTP.res_rep_header()` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1711 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 1712 | .. js:function:: HTTP.res_rep_header(http, name, regex, string) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1713 | |
| 1714 | Matches the regular expression in all occurrences of header field "name" |
| 1715 | according to "regex", and replaces them with the "replace" argument. The |
| 1716 | replacement value can contain back references like \1, \2, ... This |
| 1717 | function works with the request. |
| 1718 | |
| 1719 | :param class_http http: The related http object. |
| 1720 | :param string name: The header name. |
| 1721 | :param string regex: The match regular expression. |
| 1722 | :param string replace: The replacement value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1723 | :see: :js:func:`HTTP.req_rep_header()` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1724 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1725 | .. js:function:: HTTP.req_set_method(http, method) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1726 | |
| 1727 | Rewrites the request method with the parameter "method". |
| 1728 | |
| 1729 | :param class_http http: The related http object. |
| 1730 | :param string method: The new method. |
| 1731 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1732 | .. js:function:: HTTP.req_set_path(http, path) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1733 | |
| 1734 | Rewrites the request path with the "path" parameter. |
| 1735 | |
| 1736 | :param class_http http: The related http object. |
| 1737 | :param string path: The new path. |
| 1738 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1739 | .. js:function:: HTTP.req_set_query(http, query) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1740 | |
| 1741 | Rewrites the request's query string which appears after the first question |
| 1742 | mark ("?") with the parameter "query". |
| 1743 | |
| 1744 | :param class_http http: The related http object. |
| 1745 | :param string query: The new query. |
| 1746 | |
Thierry FOURNIER | 0d79cf6 | 2015-08-26 14:20:58 +0200 | [diff] [blame] | 1747 | .. js:function:: HTTP.req_set_uri(http, uri) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1748 | |
| 1749 | Rewrites the request URI with the parameter "uri". |
| 1750 | |
| 1751 | :param class_http http: The related http object. |
| 1752 | :param string uri: The new uri. |
| 1753 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 1754 | .. js:function:: HTTP.res_set_status(http, status [, reason]) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 1755 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 1756 | Rewrites the response status code with the parameter "code". |
| 1757 | |
| 1758 | 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] | 1759 | |
| 1760 | :param class_http http: The related http object. |
| 1761 | :param integer status: The new response status code. |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 1762 | :param string reason: The new response reason (optional). |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 1763 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1764 | .. _txn_class: |
| 1765 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1766 | TXN class |
| 1767 | ========= |
| 1768 | |
| 1769 | .. js:class:: TXN |
| 1770 | |
| 1771 | The txn class contain all the functions relative to the http or tcp |
| 1772 | transaction (Note than a tcp stream is the same than a tcp transaction, but |
| 1773 | an HTTP transaction is not the same than a tcp stream). |
| 1774 | |
| 1775 | The usage of this class permits to retrieve data from the requests, alter it |
| 1776 | and forward it. |
| 1777 | |
| 1778 | All the functions provided by this class are available in the context |
| 1779 | **sample-fetches** and **actions**. |
| 1780 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1781 | .. js:attribute:: TXN.c |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1782 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1783 | :returns: An :ref:`converters_class`. |
| 1784 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1785 | This attribute contains a Converters class object. |
| 1786 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1787 | .. js:attribute:: TXN.sc |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1788 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1789 | :returns: An :ref:`converters_class`. |
| 1790 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1791 | This attribute contains a Converters class object. The functions of |
| 1792 | this object returns always a string. |
| 1793 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1794 | .. js:attribute:: TXN.f |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1795 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1796 | :returns: An :ref:`fetches_class`. |
| 1797 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1798 | This attribute contains a Fetches class object. |
| 1799 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1800 | .. js:attribute:: TXN.sf |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1801 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1802 | :returns: An :ref:`fetches_class`. |
| 1803 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1804 | This attribute contains a Fetches class object. The functions of |
| 1805 | this object returns always a string. |
| 1806 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1807 | .. js:attribute:: TXN.req |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1808 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1809 | :returns: An :ref:`channel_class`. |
| 1810 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1811 | This attribute contains a channel class object for the request buffer. |
| 1812 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1813 | .. js:attribute:: TXN.res |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1814 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1815 | :returns: An :ref:`channel_class`. |
| 1816 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1817 | This attribute contains a channel class object for the response buffer. |
| 1818 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1819 | .. js:attribute:: TXN.http |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1820 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1821 | :returns: An :ref:`http_class`. |
| 1822 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1823 | 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] | 1824 | proxy has the "mode http" enabled. |
| 1825 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1826 | .. js:function:: TXN.log(TXN, loglevel, msg) |
| 1827 | |
| 1828 | This function sends a log. The log is sent, according with the HAProxy |
| 1829 | configuration file, on the default syslog server if it is configured and on |
| 1830 | the stderr if it is allowed. |
| 1831 | |
| 1832 | :param class_txn txn: The class txn object containing the data. |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1833 | :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] | 1834 | number between 0 and 7. |
| 1835 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1836 | :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`, |
| 1837 | :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`, |
| 1838 | :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions) |
| 1839 | :see: :js:func:`TXN.deflog` |
| 1840 | :see: :js:func:`TXN.Debug` |
| 1841 | :see: :js:func:`TXN.Info` |
| 1842 | :see: :js:func:`TXN.Warning` |
| 1843 | :see: :js:func:`TXN.Alert` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1844 | |
| 1845 | .. js:function:: TXN.deflog(TXN, msg) |
| 1846 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1847 | 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] | 1848 | transaction. |
| 1849 | |
| 1850 | :param class_txn txn: The class txn object containing the data. |
| 1851 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1852 | :see: :js:func:`TXN.log |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1853 | |
| 1854 | .. js:function:: TXN.Debug(txn, msg) |
| 1855 | |
| 1856 | :param class_txn txn: The class txn object containing the data. |
| 1857 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1858 | :see: :js:func:`TXN.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1859 | |
| 1860 | Does the same job than: |
| 1861 | |
| 1862 | .. code-block:: lua |
| 1863 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1864 | function Debug(txn, msg) |
| 1865 | TXN.log(txn, core.debug, msg) |
| 1866 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1867 | .. |
| 1868 | |
| 1869 | .. js:function:: TXN.Info(txn, msg) |
| 1870 | |
| 1871 | :param class_txn txn: The class txn object containing the data. |
| 1872 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1873 | :see: :js:func:`TXN.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1874 | |
| 1875 | .. code-block:: lua |
| 1876 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1877 | function Debug(txn, msg) |
| 1878 | TXN.log(txn, core.info, msg) |
| 1879 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1880 | .. |
| 1881 | |
| 1882 | .. js:function:: TXN.Warning(txn, msg) |
| 1883 | |
| 1884 | :param class_txn txn: The class txn object containing the data. |
| 1885 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1886 | :see: :js:func:`TXN.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1887 | |
| 1888 | .. code-block:: lua |
| 1889 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1890 | function Debug(txn, msg) |
| 1891 | TXN.log(txn, core.warning, msg) |
| 1892 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1893 | .. |
| 1894 | |
| 1895 | .. js:function:: TXN.Alert(txn, msg) |
| 1896 | |
| 1897 | :param class_txn txn: The class txn object containing the data. |
| 1898 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1899 | :see: :js:func:`TXN.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1900 | |
| 1901 | .. code-block:: lua |
| 1902 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1903 | function Debug(txn, msg) |
| 1904 | TXN.log(txn, core.alert, msg) |
| 1905 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1906 | .. |
| 1907 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1908 | .. js:function:: TXN.get_priv(txn) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1909 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1910 | 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] | 1911 | function. If no data are stored, it returns a nil value. |
| 1912 | |
| 1913 | :param class_txn txn: The class txn object containing the data. |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1914 | :returns: the opaque data previously stored, or nil if nothing is |
| 1915 | available. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1916 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1917 | .. js:function:: TXN.set_priv(txn, data) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1918 | |
| 1919 | Store any data in the current HAProxy transaction. This action replace the |
| 1920 | old stored data. |
| 1921 | |
| 1922 | :param class_txn txn: The class txn object containing the data. |
| 1923 | :param opaque data: The data which is stored in the transaction. |
| 1924 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 1925 | .. js:function:: TXN.set_var(TXN, var, value[, ifexist]) |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 1926 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 1927 | 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] | 1928 | |
| 1929 | :param class_txn txn: The class txn object containing the data. |
| 1930 | :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] | 1931 | :param type value: The value associated to the variable. The type can be string or |
| 1932 | integer. |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 1933 | :param boolean ifexist: If this parameter is set to a truthy value the variable |
| 1934 | will only be set if it was defined elsewhere (i.e. used |
| 1935 | within the configuration). It is highly recommended to |
| 1936 | always set this to true. |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 1937 | |
| 1938 | .. js:function:: TXN.unset_var(TXN, var) |
| 1939 | |
| 1940 | Unset the variable <var>. |
| 1941 | |
| 1942 | :param class_txn txn: The class txn object containing the data. |
| 1943 | :param string var: The variable name according with the HAProxy variable syntax. |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 1944 | |
| 1945 | .. js:function:: TXN.get_var(TXN, var) |
| 1946 | |
| 1947 | Returns data stored in the variable <var> converter in Lua type. |
| 1948 | |
| 1949 | :param class_txn txn: The class txn object containing the data. |
| 1950 | :param string var: The variable name according with the HAProxy variable syntax. |
| 1951 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 1952 | .. js:function:: TXN.reply([reply]) |
| 1953 | |
| 1954 | Return a new reply object |
| 1955 | |
| 1956 | :param table reply: A table containing info to initialize the reply fields. |
| 1957 | :returns: A :ref:`reply_class` object. |
| 1958 | |
| 1959 | The table used to initialized the reply object may contain following entries : |
| 1960 | |
| 1961 | * status : The reply status code. the code 200 is used by default. |
| 1962 | * reason : The reply reason. The reason corresponding to the status code is |
| 1963 | used by default. |
| 1964 | * headers : An list of headers, indexed by header name. Empty by default. For |
| 1965 | a given name, multiple values are possible, stored in an ordered list. |
| 1966 | * body : The reply body, empty by default. |
| 1967 | |
| 1968 | .. code-block:: lua |
| 1969 | |
| 1970 | local reply = txn:reply{ |
| 1971 | status = 400, |
| 1972 | reason = "Bad request", |
| 1973 | headers = { |
| 1974 | ["content-type"] = { "text/html" }, |
| 1975 | ["cache-control"] = {"no-cache", "no-store" } |
| 1976 | }, |
| 1977 | body = "<html><body><h1>invalid request<h1></body></html>" |
| 1978 | } |
| 1979 | .. |
| 1980 | :see: :js:class:`Reply` |
| 1981 | |
| 1982 | .. js:function:: TXN.done(txn[, reply]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1983 | |
Willy Tarreau | bc183a6 | 2015-08-28 10:39:11 +0200 | [diff] [blame] | 1984 | This function terminates processing of the transaction and the associated |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 1985 | session and optionally reply to the client for HTTP sessions. |
| 1986 | |
| 1987 | :param class_txn txn: The class txn object containing the data. |
| 1988 | :param class_reply reply: The class reply object to return to the client. |
| 1989 | |
| 1990 | 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] | 1991 | 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] | 1992 | To do so, a reply may be provided. This object is optional and may contain a |
| 1993 | status code, a reason, a header list and a body. All these fields are |
| 1994 | optionnals. When not provided, the default values are used. By default, with |
| 1995 | an empty reply object, an empty HTTP 200 response is returned to the |
| 1996 | client. If no reply object is provided, the transaction is terminated without |
| 1997 | any reply. |
| 1998 | |
| 1999 | The reply object may be fully created in lua or the class Reply may be used to |
| 2000 | create it. |
| 2001 | |
| 2002 | .. code-block:: lua |
| 2003 | |
| 2004 | local reply = txn:reply() |
| 2005 | reply:set_status(400, "Bad request") |
| 2006 | reply:add_header("content-type", "text/html") |
| 2007 | reply:add_header("cache-control", "no-cache") |
| 2008 | reply:add_header("cache-control", "no-store") |
| 2009 | reply:set_body("<html><body><h1>invalid request<h1></body></html>") |
| 2010 | txn:done(reply) |
| 2011 | .. |
| 2012 | |
| 2013 | .. code-block:: lua |
| 2014 | |
| 2015 | txn:done{ |
| 2016 | status = 400, |
| 2017 | reason = "Bad request", |
| 2018 | headers = { |
| 2019 | ["content-type"] = { "text/html" }, |
| 2020 | ["cache-control"] = { "no-cache", "no-store" }, |
| 2021 | }, |
| 2022 | body = "<html><body><h1>invalid request<h1></body></html>" |
| 2023 | } |
| 2024 | .. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2025 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 2026 | *Warning*: It not make sense to call this function from sample-fetches. In |
| 2027 | this case the behaviour of this one is the same than core.done(): it quit |
| 2028 | the Lua execution. The transaction is really aborted only from an action |
| 2029 | registered function. |
| 2030 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 2031 | :see: :js:func:`TXN.reply`, :js:class:`Reply` |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2032 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2033 | .. js:function:: TXN.set_loglevel(txn, loglevel) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 2034 | |
| 2035 | Is used to change the log level of the current request. The "loglevel" must |
| 2036 | be an integer between 0 and 7. |
| 2037 | |
| 2038 | :param class_txn txn: The class txn object containing the data. |
| 2039 | :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] | 2040 | :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`, |
| 2041 | :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`, |
| 2042 | :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 2043 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2044 | .. js:function:: TXN.set_tos(txn, tos) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 2045 | |
| 2046 | Is used to set the TOS or DSCP field value of packets sent to the client to |
| 2047 | the value passed in "tos" on platforms which support this. |
| 2048 | |
| 2049 | :param class_txn txn: The class txn object containing the data. |
| 2050 | :param integer tos: The new TOS os DSCP. |
| 2051 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2052 | .. js:function:: TXN.set_mark(txn, mark) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 2053 | |
| 2054 | Is used to set the Netfilter MARK on all packets sent to the client to the |
| 2055 | value passed in "mark" on platforms which support it. |
| 2056 | |
| 2057 | :param class_txn txn: The class txn object containing the data. |
| 2058 | :param integer mark: The mark value. |
| 2059 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 2060 | .. js:function:: TXN.set_priority_class(txn, prio) |
| 2061 | |
| 2062 | This function adjusts the priority class of the transaction. The value should |
| 2063 | be within the range -2047..2047. Values outside this range will be |
| 2064 | truncated. |
| 2065 | |
| 2066 | See the HAProxy configuration.txt file keyword "http-request" action |
| 2067 | "set-priority-class" for details. |
| 2068 | |
| 2069 | .. js:function:: TXN.set_priority_offset(txn, prio) |
| 2070 | |
| 2071 | This function adjusts the priority offset of the transaction. The value |
| 2072 | should be within the range -524287..524287. Values outside this range will be |
| 2073 | truncated. |
| 2074 | |
| 2075 | See the HAProxy configuration.txt file keyword "http-request" action |
| 2076 | "set-priority-offset" for details. |
| 2077 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 2078 | .. _reply_class: |
| 2079 | |
| 2080 | Reply class |
| 2081 | ============ |
| 2082 | |
| 2083 | .. js:class:: Reply |
| 2084 | |
| 2085 | **context**: action |
| 2086 | |
| 2087 | This class represents an HTTP response message. It provides some methods to |
| 2088 | enrich it. |
| 2089 | |
| 2090 | .. code-block:: lua |
| 2091 | |
| 2092 | local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used |
| 2093 | reply:add_header("content-type", "text/html") |
| 2094 | reply:add_header("cache-control", "no-cache") |
| 2095 | reply:add_header("cache-control", "no-store") |
| 2096 | reply:set_body("<html><body><h1>invalid request<h1></body></html>") |
| 2097 | .. |
| 2098 | |
| 2099 | :see: :js:func:`TXN.reply` |
| 2100 | |
| 2101 | .. js:attribute:: Reply.status |
| 2102 | |
| 2103 | The reply status code. By default, the status code is set to 200. |
| 2104 | |
| 2105 | :returns: integer |
| 2106 | |
| 2107 | .. js:attribute:: Reply.reason |
| 2108 | |
| 2109 | The reason string describing the status code. |
| 2110 | |
| 2111 | :returns: string |
| 2112 | |
| 2113 | .. js:attribute:: Reply.headers |
| 2114 | |
| 2115 | A table indexing all reply headers by name. To each name is associated an |
| 2116 | ordered list of values. |
| 2117 | |
| 2118 | :returns: Lua table |
| 2119 | |
| 2120 | .. code-block:: lua |
| 2121 | |
| 2122 | { |
| 2123 | ["content-type"] = { "text/html" }, |
| 2124 | ["cache-control"] = {"no-cache", "no-store" }, |
| 2125 | x_header_name = { "value1", "value2", ... } |
| 2126 | ... |
| 2127 | } |
| 2128 | .. |
| 2129 | |
| 2130 | .. js:attribute:: Reply.body |
| 2131 | |
| 2132 | The reply payload. |
| 2133 | |
| 2134 | :returns: string |
| 2135 | |
| 2136 | .. js:function:: Reply.set_status(REPLY, status[, reason]) |
| 2137 | |
| 2138 | Set the reply status code and optionally the reason-phrase. If the reason is |
| 2139 | not provided, the default reason corresponding to the status code is used. |
| 2140 | |
| 2141 | :param class_reply reply: The related Reply object. |
| 2142 | :param integer status: The reply status code. |
| 2143 | :param string reason: The reply status reason (optional). |
| 2144 | |
| 2145 | .. js:function:: Reply.add_header(REPLY, name, value) |
| 2146 | |
| 2147 | Add a header to the reply object. If the header does not already exist, a new |
| 2148 | entry is created with its name as index and a one-element list containing its |
| 2149 | value as value. Otherwise, the header value is appended to the ordered list of |
| 2150 | values associated to the header name. |
| 2151 | |
| 2152 | :param class_reply reply: The related Reply object. |
| 2153 | :param string name: The header field name. |
| 2154 | :param string value: The header field value. |
| 2155 | |
| 2156 | .. js:function:: Reply.del_header(REPLY, name) |
| 2157 | |
| 2158 | Remove all occurrences of a header name from the reply object. |
| 2159 | |
| 2160 | :param class_reply reply: The related Reply object. |
| 2161 | :param string name: The header field name. |
| 2162 | |
| 2163 | .. js:function:: Reply.set_body(REPLY, body) |
| 2164 | |
| 2165 | Set the reply payload. |
| 2166 | |
| 2167 | :param class_reply reply: The related Reply object. |
| 2168 | :param string body: The reply payload. |
| 2169 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2170 | .. _socket_class: |
| 2171 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2172 | Socket class |
| 2173 | ============ |
| 2174 | |
| 2175 | .. js:class:: Socket |
| 2176 | |
| 2177 | This class must be compatible with the Lua Socket class. Only the 'client' |
| 2178 | functions are available. See the Lua Socket documentation: |
| 2179 | |
| 2180 | `http://w3.impa.br/~diego/software/luasocket/tcp.html |
| 2181 | <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_ |
| 2182 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2183 | .. js:function:: Socket.close(socket) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2184 | |
| 2185 | Closes a TCP object. The internal socket used by the object is closed and the |
| 2186 | local address to which the object was bound is made available to other |
| 2187 | applications. No further operations (except for further calls to the close |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2188 | method) are allowed on a closed Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2189 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2190 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2191 | |
| 2192 | Note: It is important to close all used sockets once they are not needed, |
| 2193 | since, in many systems, each socket uses a file descriptor, which are limited |
| 2194 | system resources. Garbage-collected objects are automatically closed before |
| 2195 | destruction, though. |
| 2196 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2197 | .. js:function:: Socket.connect(socket, address[, port]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2198 | |
| 2199 | Attempts to connect a socket object to a remote host. |
| 2200 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2201 | |
| 2202 | In case of error, the method returns nil followed by a string describing the |
| 2203 | error. In case of success, the method returns 1. |
| 2204 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2205 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2206 | :param string address: can be an IP address or a host name. See below for more |
| 2207 | information. |
| 2208 | :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] | 2209 | :returns: 1 or nil. |
| 2210 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 2211 | 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] | 2212 | other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is |
| 2213 | the basically expected format. This format requires the port. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2214 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2215 | 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] | 2216 | connect to a socket. Abstract namespaces are supported with the prefix |
Joseph Herlant | 02cedc4 | 2018-11-13 19:45:17 -0800 | [diff] [blame] | 2217 | "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] | 2218 | 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] | 2219 | 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] | 2220 | parameter *port* must not be set. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2221 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2222 | .. js:function:: Socket.connect_ssl(socket, address, port) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2223 | |
| 2224 | Same behavior than the function socket:connect, but uses SSL. |
| 2225 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2226 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2227 | :returns: 1 or nil. |
| 2228 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2229 | .. js:function:: Socket.getpeername(socket) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2230 | |
| 2231 | Returns information about the remote side of a connected client object. |
| 2232 | |
| 2233 | Returns a string with the IP address of the peer, followed by the port number |
| 2234 | that peer is using for the connection. In case of error, the method returns |
| 2235 | nil. |
| 2236 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2237 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2238 | :returns: a string containing the server information. |
| 2239 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2240 | .. js:function:: Socket.getsockname(socket) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2241 | |
| 2242 | Returns the local address information associated to the object. |
| 2243 | |
| 2244 | The method returns a string with local IP address and a number with the port. |
| 2245 | In case of error, the method returns nil. |
| 2246 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2247 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2248 | :returns: a string containing the client information. |
| 2249 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2250 | .. js:function:: Socket.receive(socket, [pattern [, prefix]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2251 | |
| 2252 | Reads data from a client object, according to the specified read pattern. |
| 2253 | Patterns follow the Lua file I/O format, and the difference in performance |
| 2254 | between all patterns is negligible. |
| 2255 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2256 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2257 | :param string|integer pattern: Describe what is required (see below). |
| 2258 | :param string prefix: A string which will be prefix the returned data. |
| 2259 | :returns: a string containing the required data or nil. |
| 2260 | |
| 2261 | Pattern can be any of the following: |
| 2262 | |
| 2263 | * **`*a`**: reads from the socket until the connection is closed. No |
| 2264 | end-of-line translation is performed; |
| 2265 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2266 | * **`*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] | 2267 | LF character (ASCII 10), optionally preceded by a CR character |
| 2268 | (ASCII 13). The CR and LF characters are not included in the |
| 2269 | returned line. In fact, all CR characters are ignored by the |
| 2270 | pattern. This is the default pattern. |
| 2271 | |
| 2272 | * **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] | 2273 | Socket. Prefix is an optional string to be concatenated to the |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2274 | beginning of any received data before return. |
| 2275 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2276 | * **empty**: If the pattern is left empty, the default option is `*l`. |
| 2277 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2278 | If successful, the method returns the received pattern. In case of error, the |
| 2279 | method returns nil followed by an error message which can be the string |
| 2280 | 'closed' in case the connection was closed before the transmission was |
| 2281 | completed or the string 'timeout' in case there was a timeout during the |
| 2282 | operation. Also, after the error message, the function returns the partial |
| 2283 | result of the transmission. |
| 2284 | |
| 2285 | Important note: This function was changed severely. It used to support |
| 2286 | multiple patterns (but I have never seen this feature used) and now it |
| 2287 | doesn't anymore. Partial results used to be returned in the same way as |
| 2288 | successful results. This last feature violated the idea that all functions |
| 2289 | should return nil on error. Thus it was changed too. |
| 2290 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2291 | .. js:function:: Socket.send(socket, data [, start [, end ]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2292 | |
| 2293 | Sends data through client object. |
| 2294 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2295 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2296 | :param string data: The data that will be sent. |
| 2297 | :param integer start: The start position in the buffer of the data which will |
| 2298 | be sent. |
| 2299 | :param integer end: The end position in the buffer of the data which will |
| 2300 | be sent. |
| 2301 | :returns: see below. |
| 2302 | |
| 2303 | Data is the string to be sent. The optional arguments i and j work exactly |
| 2304 | like the standard string.sub Lua function to allow the selection of a |
| 2305 | substring to be sent. |
| 2306 | |
| 2307 | If successful, the method returns the index of the last byte within [start, |
| 2308 | end] that has been sent. Notice that, if start is 1 or absent, this is |
| 2309 | effectively the total number of bytes sent. In case of error, the method |
| 2310 | returns nil, followed by an error message, followed by the index of the last |
| 2311 | byte within [start, end] that has been sent. You might want to try again from |
| 2312 | the byte following that. The error message can be 'closed' in case the |
| 2313 | connection was closed before the transmission was completed or the string |
| 2314 | 'timeout' in case there was a timeout during the operation. |
| 2315 | |
| 2316 | Note: Output is not buffered. For small strings, it is always better to |
| 2317 | concatenate them in Lua (with the '..' operator) and send the result in one |
| 2318 | call instead of calling the method several times. |
| 2319 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2320 | .. js:function:: Socket.setoption(socket, option [, value]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2321 | |
| 2322 | Just implemented for compatibility, this cal does nothing. |
| 2323 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2324 | .. js:function:: Socket.settimeout(socket, value [, mode]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2325 | |
| 2326 | Changes the timeout values for the object. All I/O operations are blocking. |
| 2327 | That is, any call to the methods send, receive, and accept will block |
| 2328 | indefinitely, until the operation completes. The settimeout method defines a |
| 2329 | limit on the amount of time the I/O methods can block. When a timeout time |
| 2330 | has elapsed, the affected methods give up and fail with an error code. |
| 2331 | |
| 2332 | The amount of time to wait is specified as the value parameter, in seconds. |
| 2333 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2334 | The timeout modes are not implemented, the only settable timeout is the |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2335 | inactivity time waiting for complete the internal buffer send or waiting for |
| 2336 | receive data. |
| 2337 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2338 | :param class_socket socket: Is the manipulated Socket. |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 2339 | :param float value: The timeout value. Use floating point to specify |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2340 | milliseconds. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2341 | |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 2342 | .. _regex_class: |
| 2343 | |
| 2344 | Regex class |
| 2345 | =========== |
| 2346 | |
| 2347 | .. js:class:: Regex |
| 2348 | |
| 2349 | This class allows the usage of HAProxy regexes because classic lua doesn't |
| 2350 | provides regexes. This class inherits the HAProxy compilation options, so the |
| 2351 | regexes can be libc regex, pcre regex or pcre JIT regex. |
| 2352 | |
| 2353 | The expression matching number is limited to 20 per regex. The only available |
| 2354 | option is case sensitive. |
| 2355 | |
| 2356 | Because regexes compilation is a heavy process, it is better to define all |
| 2357 | your regexes in the **body context** and use it during the runtime. |
| 2358 | |
| 2359 | .. code-block:: lua |
| 2360 | |
| 2361 | -- Create the regex |
| 2362 | st, regex = Regex.new("needle (..) (...)", true); |
| 2363 | |
| 2364 | -- Check compilation errors |
| 2365 | if st == false then |
| 2366 | print "error: " .. regex |
| 2367 | end |
| 2368 | |
| 2369 | -- Match the regexes |
| 2370 | print(regex:exec("Looking for a needle in the haystack")) -- true |
| 2371 | print(regex:exec("Lokking for a cat in the haystack")) -- false |
| 2372 | |
| 2373 | -- Extract words |
| 2374 | st, list = regex:match("Looking for a needle in the haystack") |
| 2375 | print(st) -- true |
| 2376 | print(list[1]) -- needle in the |
| 2377 | print(list[2]) -- in |
| 2378 | print(list[3]) -- the |
| 2379 | |
| 2380 | .. js:function:: Regex.new(regex, case_sensitive) |
| 2381 | |
| 2382 | Create and compile a regex. |
| 2383 | |
| 2384 | :param string regex: The regular expression according with the libc or pcre |
| 2385 | standard |
| 2386 | :param boolean case_sensitive: Match is case sensitive or not. |
| 2387 | :returns: boolean status and :ref:`regex_class` or string containing fail reason. |
| 2388 | |
| 2389 | .. js:function:: Regex.exec(regex, str) |
| 2390 | |
| 2391 | Execute the regex. |
| 2392 | |
| 2393 | :param class_regex regex: A :ref:`regex_class` object. |
| 2394 | :param string str: The input string will be compared with the compiled regex. |
| 2395 | :returns: a boolean status according with the match result. |
| 2396 | |
| 2397 | .. js:function:: Regex.match(regex, str) |
| 2398 | |
| 2399 | Execute the regex and return matched expressions. |
| 2400 | |
| 2401 | :param class_map map: A :ref:`regex_class` object. |
| 2402 | :param string str: The input string will be compared with the compiled regex. |
| 2403 | :returns: a boolean status according with the match result, and |
| 2404 | a table containing all the string matched in order of declaration. |
| 2405 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2406 | .. _map_class: |
| 2407 | |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2408 | Map class |
| 2409 | ========= |
| 2410 | |
| 2411 | .. js:class:: Map |
| 2412 | |
| 2413 | 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] | 2414 | be modified during the runtime through the HAProxy management socket. |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2415 | |
| 2416 | .. code-block:: lua |
| 2417 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2418 | default = "usa" |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2419 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2420 | -- Create and load map |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2421 | geo = Map.new("geo.map", Map._ip); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2422 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2423 | -- Create new fetch that returns the user country |
| 2424 | core.register_fetches("country", function(txn) |
| 2425 | local src; |
| 2426 | local loc; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2427 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2428 | src = txn.f:fhdr("x-forwarded-for"); |
| 2429 | if (src == nil) then |
| 2430 | src = txn.f:src() |
| 2431 | if (src == nil) then |
| 2432 | return default; |
| 2433 | end |
| 2434 | end |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2435 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2436 | -- Perform lookup |
| 2437 | loc = geo:lookup(src); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2438 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2439 | if (loc == nil) then |
| 2440 | return default; |
| 2441 | end |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2442 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2443 | return loc; |
| 2444 | end); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2445 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2446 | .. js:attribute:: Map._int |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2447 | |
| 2448 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2449 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2450 | method. |
| 2451 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2452 | Note that :js:attr:`Map.int` is also available for compatibility. |
| 2453 | |
| 2454 | .. js:attribute:: Map._ip |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2455 | |
| 2456 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2457 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2458 | method. |
| 2459 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2460 | Note that :js:attr:`Map.ip` is also available for compatibility. |
| 2461 | |
| 2462 | .. js:attribute:: Map._str |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2463 | |
| 2464 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2465 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2466 | method. |
| 2467 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2468 | Note that :js:attr:`Map.str` is also available for compatibility. |
| 2469 | |
| 2470 | .. js:attribute:: Map._beg |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2471 | |
| 2472 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2473 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2474 | method. |
| 2475 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2476 | Note that :js:attr:`Map.beg` is also available for compatibility. |
| 2477 | |
| 2478 | .. js:attribute:: Map._sub |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2479 | |
| 2480 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2481 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2482 | method. |
| 2483 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2484 | Note that :js:attr:`Map.sub` is also available for compatibility. |
| 2485 | |
| 2486 | .. js:attribute:: Map._dir |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2487 | |
| 2488 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2489 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2490 | method. |
| 2491 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2492 | Note that :js:attr:`Map.dir` is also available for compatibility. |
| 2493 | |
| 2494 | .. js:attribute:: Map._dom |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2495 | |
| 2496 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2497 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2498 | method. |
| 2499 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2500 | Note that :js:attr:`Map.dom` is also available for compatibility. |
| 2501 | |
| 2502 | .. js:attribute:: Map._end |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2503 | |
| 2504 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2505 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2506 | method. |
| 2507 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2508 | .. js:attribute:: Map._reg |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2509 | |
| 2510 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2511 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2512 | method. |
| 2513 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2514 | Note that :js:attr:`Map.reg` is also available for compatibility. |
| 2515 | |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2516 | |
| 2517 | .. js:function:: Map.new(file, method) |
| 2518 | |
| 2519 | Creates and load a map. |
| 2520 | |
| 2521 | :param string file: Is the file containing the map. |
| 2522 | :param integer method: Is the map pattern matching method. See the attributes |
| 2523 | of the Map class. |
| 2524 | :returns: a class Map object. |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2525 | :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`, |
| 2526 | :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`, |
| 2527 | :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and |
| 2528 | :js:attr:`Map._reg`. |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2529 | |
| 2530 | .. js:function:: Map.lookup(map, str) |
| 2531 | |
| 2532 | Perform a lookup in a map. |
| 2533 | |
| 2534 | :param class_map map: Is the class Map object. |
| 2535 | :param string str: Is the string used as key. |
| 2536 | :returns: a string containing the result or nil if no match. |
| 2537 | |
| 2538 | .. js:function:: Map.slookup(map, str) |
| 2539 | |
| 2540 | Perform a lookup in a map. |
| 2541 | |
| 2542 | :param class_map map: Is the class Map object. |
| 2543 | :param string str: Is the string used as key. |
| 2544 | :returns: a string containing the result or empty string if no match. |
| 2545 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2546 | .. _applethttp_class: |
| 2547 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2548 | AppletHTTP class |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2549 | ================ |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2550 | |
| 2551 | .. js:class:: AppletHTTP |
| 2552 | |
| 2553 | This class is used with applets that requires the 'http' mode. The http applet |
| 2554 | can be registered with the *core.register_service()* function. They are used |
| 2555 | for processing an http request like a server in back of HAProxy. |
| 2556 | |
| 2557 | This is an hello world sample code: |
| 2558 | |
| 2559 | .. code-block:: lua |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2560 | |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 2561 | core.register_service("hello-world", "http", function(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2562 | local response = "Hello World !" |
| 2563 | applet:set_status(200) |
Pieter Baauw | 2dcb9bc | 2015-10-01 22:47:12 +0200 | [diff] [blame] | 2564 | applet:add_header("content-length", string.len(response)) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2565 | applet:add_header("content-type", "text/plain") |
Pieter Baauw | 2dcb9bc | 2015-10-01 22:47:12 +0200 | [diff] [blame] | 2566 | applet:start_response() |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2567 | applet:send(response) |
| 2568 | end) |
| 2569 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2570 | .. js:attribute:: AppletHTTP.c |
| 2571 | |
| 2572 | :returns: A :ref:`converters_class` |
| 2573 | |
| 2574 | This attribute contains a Converters class object. |
| 2575 | |
| 2576 | .. js:attribute:: AppletHTTP.sc |
| 2577 | |
| 2578 | :returns: A :ref:`converters_class` |
| 2579 | |
| 2580 | This attribute contains a Converters class object. The |
| 2581 | functions of this object returns always a string. |
| 2582 | |
| 2583 | .. js:attribute:: AppletHTTP.f |
| 2584 | |
| 2585 | :returns: A :ref:`fetches_class` |
| 2586 | |
| 2587 | This attribute contains a Fetches class object. Note that the |
| 2588 | applet execution place cannot access to a valid HAProxy core HTTP |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2589 | transaction, so some sample fetches related to the HTTP dependent |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2590 | values (hdr, path, ...) are not available. |
| 2591 | |
| 2592 | .. js:attribute:: AppletHTTP.sf |
| 2593 | |
| 2594 | :returns: A :ref:`fetches_class` |
| 2595 | |
| 2596 | This attribute contains a Fetches class object. The functions of |
| 2597 | this object returns always a string. Note that the applet |
| 2598 | execution place cannot access to a valid HAProxy core HTTP |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2599 | transaction, so some sample fetches related to the HTTP dependent |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2600 | values (hdr, path, ...) are not available. |
| 2601 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2602 | .. js:attribute:: AppletHTTP.method |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2603 | |
| 2604 | :returns: string |
| 2605 | |
| 2606 | The attribute method returns a string containing the HTTP |
| 2607 | method. |
| 2608 | |
| 2609 | .. js:attribute:: AppletHTTP.version |
| 2610 | |
| 2611 | :returns: string |
| 2612 | |
| 2613 | The attribute version, returns a string containing the HTTP |
| 2614 | request version. |
| 2615 | |
| 2616 | .. js:attribute:: AppletHTTP.path |
| 2617 | |
| 2618 | :returns: string |
| 2619 | |
| 2620 | The attribute path returns a string containing the HTTP |
| 2621 | request path. |
| 2622 | |
| 2623 | .. js:attribute:: AppletHTTP.qs |
| 2624 | |
| 2625 | :returns: string |
| 2626 | |
| 2627 | The attribute qs returns a string containing the HTTP |
| 2628 | request query string. |
| 2629 | |
| 2630 | .. js:attribute:: AppletHTTP.length |
| 2631 | |
| 2632 | :returns: integer |
| 2633 | |
| 2634 | The attribute length returns an integer containing the HTTP |
| 2635 | body length. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2636 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 2637 | .. js:attribute:: AppletHTTP.headers |
| 2638 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 2639 | :returns: table |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2640 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 2641 | The attribute headers returns a table containing the HTTP |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2642 | headers. The header names are always in lower case. As the header name can be |
| 2643 | 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] | 2644 | first index value. The table have this form: |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2645 | |
| 2646 | .. code-block:: lua |
| 2647 | |
| 2648 | AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>" |
| 2649 | |
| 2650 | AppletHTTP.headers["host"][0] = "www.test.com" |
| 2651 | AppletHTTP.headers["accept"][0] = "audio/basic q=1" |
| 2652 | AppletHTTP.headers["accept"][1] = "audio/*, q=0.2" |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2653 | AppletHTTP.headers["accept"][2] = "*/*, q=0.1" |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2654 | .. |
| 2655 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 2656 | .. js:function:: AppletHTTP.set_status(applet, code [, reason]) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2657 | |
| 2658 | This function sets the HTTP status code for the response. The allowed code are |
| 2659 | from 100 to 599. |
| 2660 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2661 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2662 | :param integer code: the status code returned to the client. |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 2663 | :param string reason: the status reason returned to the client (optional). |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2664 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2665 | .. js:function:: AppletHTTP.add_header(applet, name, value) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2666 | |
| 2667 | This function add an header in the response. Duplicated headers are not |
| 2668 | collapsed. The special header *content-length* is used to determinate the |
| 2669 | 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] | 2670 | all the write from the function *AppletHTTP:send()* become a chunk. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2671 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2672 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2673 | :param string name: the header name |
| 2674 | :param string value: the header value |
| 2675 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2676 | .. js:function:: AppletHTTP.start_response(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2677 | |
| 2678 | This function indicates to the HTTP engine that it can process and send the |
| 2679 | response headers. After this called we cannot add headers to the response; We |
| 2680 | cannot use the *AppletHTTP:send()* function if the |
| 2681 | *AppletHTTP:start_response()* is not called. |
| 2682 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2683 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 2684 | |
| 2685 | .. js:function:: AppletHTTP.getline(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2686 | |
| 2687 | This function returns a string containing one line from the http body. If the |
| 2688 | data returned doesn't contains a final '\\n' its assumed than its the last |
| 2689 | available data before the end of stream. |
| 2690 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2691 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2692 | :returns: a string. The string can be empty if we reach the end of the stream. |
| 2693 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2694 | .. js:function:: AppletHTTP.receive(applet, [size]) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2695 | |
| 2696 | Reads data from the HTTP body, according to the specified read *size*. If the |
| 2697 | *size* is missing, the function tries to read all the content of the stream |
| 2698 | 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] | 2699 | amount of data available. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2700 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2701 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2702 | :param integer size: the required read size. |
Ilya Shipitsin | 11057a3 | 2020-06-21 21:18:27 +0500 | [diff] [blame] | 2703 | :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] | 2704 | closed. |
| 2705 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2706 | .. js:function:: AppletHTTP.send(applet, msg) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2707 | |
| 2708 | Send the message *msg* on the http request body. |
| 2709 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2710 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2711 | :param string msg: the message to send. |
| 2712 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 2713 | .. js:function:: AppletHTTP.get_priv(applet) |
| 2714 | |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2715 | Return Lua data stored in the current transaction. If no data are stored, |
| 2716 | it returns a nil value. |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 2717 | |
| 2718 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 2719 | :returns: the opaque data previously stored, or nil if nothing is |
| 2720 | available. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2721 | :see: :js:func:`AppletHTTP.set_priv` |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 2722 | |
| 2723 | .. js:function:: AppletHTTP.set_priv(applet, data) |
| 2724 | |
| 2725 | Store any data in the current HAProxy transaction. This action replace the |
| 2726 | old stored data. |
| 2727 | |
| 2728 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 2729 | :param opaque data: The data which is stored in the transaction. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2730 | :see: :js:func:`AppletHTTP.get_priv` |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 2731 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 2732 | .. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist]) |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 2733 | |
| 2734 | Converts a Lua type in a HAProxy type and store it in a variable <var>. |
| 2735 | |
| 2736 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 2737 | :param string var: The variable name according with the HAProxy variable syntax. |
| 2738 | :param type value: The value associated to the variable. The type ca be string or |
| 2739 | integer. |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 2740 | :param boolean ifexist: If this parameter is set to a truthy value the variable |
| 2741 | will only be set if it was defined elsewhere (i.e. used |
| 2742 | within the configuration). It is highly recommended to |
| 2743 | always set this to true. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2744 | :see: :js:func:`AppletHTTP.unset_var` |
| 2745 | :see: :js:func:`AppletHTTP.get_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 2746 | |
| 2747 | .. js:function:: AppletHTTP.unset_var(applet, var) |
| 2748 | |
| 2749 | Unset the variable <var>. |
| 2750 | |
| 2751 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 2752 | :param string var: The variable name according with the HAProxy variable syntax. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2753 | :see: :js:func:`AppletHTTP.set_var` |
| 2754 | :see: :js:func:`AppletHTTP.get_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 2755 | |
| 2756 | .. js:function:: AppletHTTP.get_var(applet, var) |
| 2757 | |
| 2758 | Returns data stored in the variable <var> converter in Lua type. |
| 2759 | |
| 2760 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 2761 | :param string var: The variable name according with the HAProxy variable syntax. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2762 | :see: :js:func:`AppletHTTP.set_var` |
| 2763 | :see: :js:func:`AppletHTTP.unset_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 2764 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2765 | .. _applettcp_class: |
| 2766 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2767 | AppletTCP class |
| 2768 | =============== |
| 2769 | |
| 2770 | .. js:class:: AppletTCP |
| 2771 | |
| 2772 | This class is used with applets that requires the 'tcp' mode. The tcp applet |
| 2773 | can be registered with the *core.register_service()* function. They are used |
| 2774 | for processing a tcp stream like a server in back of HAProxy. |
| 2775 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2776 | .. js:attribute:: AppletTCP.c |
| 2777 | |
| 2778 | :returns: A :ref:`converters_class` |
| 2779 | |
| 2780 | This attribute contains a Converters class object. |
| 2781 | |
| 2782 | .. js:attribute:: AppletTCP.sc |
| 2783 | |
| 2784 | :returns: A :ref:`converters_class` |
| 2785 | |
| 2786 | This attribute contains a Converters class object. The |
| 2787 | functions of this object returns always a string. |
| 2788 | |
| 2789 | .. js:attribute:: AppletTCP.f |
| 2790 | |
| 2791 | :returns: A :ref:`fetches_class` |
| 2792 | |
| 2793 | This attribute contains a Fetches class object. |
| 2794 | |
| 2795 | .. js:attribute:: AppletTCP.sf |
| 2796 | |
| 2797 | :returns: A :ref:`fetches_class` |
| 2798 | |
| 2799 | This attribute contains a Fetches class object. |
| 2800 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2801 | .. js:function:: AppletTCP.getline(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2802 | |
| 2803 | This function returns a string containing one line from the stream. If the |
| 2804 | data returned doesn't contains a final '\\n' its assumed than its the last |
| 2805 | available data before the end of stream. |
| 2806 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2807 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2808 | :returns: a string. The string can be empty if we reach the end of the stream. |
| 2809 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2810 | .. js:function:: AppletTCP.receive(applet, [size]) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2811 | |
| 2812 | Reads data from the TCP stream, according to the specified read *size*. If the |
| 2813 | *size* is missing, the function tries to read all the content of the stream |
| 2814 | until the end. |
| 2815 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2816 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2817 | :param integer size: the required read size. |
Ilya Shipitsin | 11057a3 | 2020-06-21 21:18:27 +0500 | [diff] [blame] | 2818 | :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] | 2819 | closed. |
| 2820 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2821 | .. js:function:: AppletTCP.send(appletmsg) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2822 | |
| 2823 | Send the message on the stream. |
| 2824 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2825 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2826 | :param string msg: the message to send. |
| 2827 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 2828 | .. js:function:: AppletTCP.get_priv(applet) |
| 2829 | |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2830 | Return Lua data stored in the current transaction. If no data are stored, |
| 2831 | it returns a nil value. |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 2832 | |
| 2833 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 2834 | :returns: the opaque data previously stored, or nil if nothing is |
| 2835 | available. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2836 | :see: :js:func:`AppletTCP.set_priv` |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 2837 | |
| 2838 | .. js:function:: AppletTCP.set_priv(applet, data) |
| 2839 | |
| 2840 | Store any data in the current HAProxy transaction. This action replace the |
| 2841 | old stored data. |
| 2842 | |
| 2843 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
| 2844 | :param opaque data: The data which is stored in the transaction. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2845 | :see: :js:func:`AppletTCP.get_priv` |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 2846 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 2847 | .. js:function:: AppletTCP.set_var(applet, var, value[, ifexist]) |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 2848 | |
| 2849 | Converts a Lua type in a HAProxy type and stores it in a variable <var>. |
| 2850 | |
| 2851 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
| 2852 | :param string var: The variable name according with the HAProxy variable syntax. |
| 2853 | :param type value: The value associated to the variable. The type can be string or |
| 2854 | integer. |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 2855 | :param boolean ifexist: If this parameter is set to a truthy value the variable |
| 2856 | will only be set if it was defined elsewhere (i.e. used |
| 2857 | within the configuration). It is highly recommended to |
| 2858 | always set this to true. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2859 | :see: :js:func:`AppletTCP.unset_var` |
| 2860 | :see: :js:func:`AppletTCP.get_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 2861 | |
| 2862 | .. js:function:: AppletTCP.unset_var(applet, var) |
| 2863 | |
| 2864 | Unsets the variable <var>. |
| 2865 | |
| 2866 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
| 2867 | :param string var: The variable name according with the HAProxy variable syntax. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2868 | :see: :js:func:`AppletTCP.unset_var` |
| 2869 | :see: :js:func:`AppletTCP.set_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 2870 | |
| 2871 | .. js:function:: AppletTCP.get_var(applet, var) |
| 2872 | |
| 2873 | Returns data stored in the variable <var> converter in Lua type. |
| 2874 | |
| 2875 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
| 2876 | :param string var: The variable name according with the HAProxy variable syntax. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2877 | :see: :js:func:`AppletTCP.unset_var` |
| 2878 | :see: :js:func:`AppletTCP.set_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 2879 | |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 2880 | StickTable class |
| 2881 | ================ |
| 2882 | |
| 2883 | .. js:class:: StickTable |
| 2884 | |
| 2885 | **context**: task, action, sample-fetch |
| 2886 | |
| 2887 | This class can be used to access the HAProxy stick tables from Lua. |
| 2888 | |
| 2889 | .. js:function:: StickTable.info() |
| 2890 | |
| 2891 | Returns stick table attributes as a Lua table. See HAProxy documentation for |
Ilya Shipitsin | 2272d8a | 2020-12-21 01:22:40 +0500 | [diff] [blame] | 2892 | "stick-table" for canonical info, or check out example below. |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 2893 | |
| 2894 | :returns: Lua table |
| 2895 | |
| 2896 | Assume our table has IPv4 key and gpc0 and conn_rate "columns": |
| 2897 | |
| 2898 | .. code-block:: lua |
| 2899 | |
| 2900 | { |
| 2901 | expire=<int>, # Value in ms |
| 2902 | size=<int>, # Maximum table size |
| 2903 | used=<int>, # Actual number of entries in table |
| 2904 | data={ # Data columns, with types as key, and periods as values |
| 2905 | (-1 if type is not rate counter) |
| 2906 | conn_rate=<int>, |
| 2907 | gpc0=-1 |
| 2908 | }, |
| 2909 | length=<int>, # max string length for string table keys, key length |
| 2910 | # otherwise |
| 2911 | nopurge=<boolean>, # purge oldest entries when table is full |
| 2912 | type="ip" # can be "ip", "ipv6", "integer", "string", "binary" |
| 2913 | } |
| 2914 | |
| 2915 | .. js:function:: StickTable.lookup(key) |
| 2916 | |
| 2917 | Returns stick table entry for given <key> |
| 2918 | |
| 2919 | :param string key: Stick table key (IP addresses and strings are supported) |
| 2920 | :returns: Lua table |
| 2921 | |
| 2922 | .. js:function:: StickTable.dump([filter]) |
| 2923 | |
| 2924 | Returns all entries in stick table. An optional filter can be used |
| 2925 | to extract entries with specific data values. Filter is a table with valid |
| 2926 | comparison operators as keys followed by data type name and value pairs. |
| 2927 | Check out the HAProxy docs for "show table" for more details. For the |
| 2928 | reference, the supported operators are: |
| 2929 | "eq", "ne", "le", "lt", "ge", "gt" |
| 2930 | |
| 2931 | For large tables, execution of this function can take a long time (for |
| 2932 | HAProxy standards). That's also true when filter is used, so take care and |
| 2933 | measure the impact. |
| 2934 | |
| 2935 | :param table filter: Stick table filter |
| 2936 | :returns: Stick table entries (table) |
| 2937 | |
| 2938 | See below for example filter, which contains 4 entries (or comparisons). |
| 2939 | (Maximum number of filter entries is 4, defined in the source code) |
| 2940 | |
| 2941 | .. code-block:: lua |
| 2942 | |
| 2943 | local filter = { |
| 2944 | {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10} |
| 2945 | } |
| 2946 | |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 2947 | .. _action_class: |
| 2948 | |
| 2949 | Action class |
| 2950 | ============= |
| 2951 | |
| 2952 | .. js:class:: Act |
| 2953 | |
| 2954 | **context**: action |
| 2955 | |
| 2956 | This class contains all return codes an action may return. It is the lua |
| 2957 | equivalent to HAProxy "ACT_RET_*" code. |
| 2958 | |
| 2959 | .. code-block:: lua |
| 2960 | |
| 2961 | core.register_action("deny", { "http-req" }, function (txn) |
| 2962 | return act.DENY |
| 2963 | end) |
| 2964 | .. |
| 2965 | .. js:attribute:: act.CONTINUE |
| 2966 | |
| 2967 | This attribute is an integer (0). It instructs HAProxy to continue the current |
| 2968 | ruleset processing on the message. It is the default return code for a lua |
| 2969 | action. |
| 2970 | |
| 2971 | :returns: integer |
| 2972 | |
| 2973 | .. js:attribute:: act.STOP |
| 2974 | |
| 2975 | This attribute is an integer (1). It instructs HAProxy to stop the current |
| 2976 | ruleset processing on the message. |
| 2977 | |
| 2978 | .. js:attribute:: act.YIELD |
| 2979 | |
| 2980 | This attribute is an integer (2). It instructs HAProxy to temporarily pause |
| 2981 | the message processing. It will be resumed later on the same rule. The |
| 2982 | corresponding lua script is re-executed for the start. |
| 2983 | |
| 2984 | .. js:attribute:: act.ERROR |
| 2985 | |
| 2986 | This attribute is an integer (3). It triggers an internal errors The message |
| 2987 | processing is stopped and the transaction is terminated. For HTTP streams, an |
| 2988 | HTTP 500 error is returned to the client. |
| 2989 | |
| 2990 | :returns: integer |
| 2991 | |
| 2992 | .. js:attribute:: act.DONE |
| 2993 | |
| 2994 | This attribute is an integer (4). It instructs HAProxy to stop the message |
| 2995 | processing. |
| 2996 | |
| 2997 | :returns: integer |
| 2998 | |
| 2999 | .. js:attribute:: act.DENY |
| 3000 | |
| 3001 | This attribute is an integer (5). It denies the current message. The message |
| 3002 | processing is stopped and the transaction is terminated. For HTTP streams, an |
| 3003 | HTTP 403 error is returned to the client if the deny is returned during the |
| 3004 | request analysis. During the response analysis, an HTTP 502 error is returned |
| 3005 | and the server response is discarded. |
| 3006 | |
| 3007 | :returns: integer |
| 3008 | |
| 3009 | .. js:attribute:: act.ABORT |
| 3010 | |
| 3011 | This attribute is an integer (6). It aborts the current message. The message |
| 3012 | processing is stopped and the transaction is terminated. For HTTP streams, |
Willy Tarreau | 714f345 | 2021-05-09 06:47:26 +0200 | [diff] [blame] | 3013 | HAProxy assumes a response was already sent to the client. From the Lua |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 3014 | actions point of view, when this code is used, the transaction is terminated |
| 3015 | with no reply. |
| 3016 | |
| 3017 | :returns: integer |
| 3018 | |
| 3019 | .. js:attribute:: act.INVALID |
| 3020 | |
| 3021 | This attribute is an integer (7). It triggers an internal errors. The message |
| 3022 | processing is stopped and the transaction is terminated. For HTTP streams, an |
| 3023 | HTTP 400 error is returned to the client if the error is returned during the |
| 3024 | request analysis. During the response analysis, an HTTP 502 error is returned |
| 3025 | and the server response is discarded. |
| 3026 | |
| 3027 | :returns: integer |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 3028 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 3029 | .. js:function:: act:wake_time(milliseconds) |
| 3030 | |
| 3031 | **context**: action |
| 3032 | |
| 3033 | Set the script pause timeout to the specified time, defined in |
| 3034 | milliseconds. |
| 3035 | |
| 3036 | :param integer milliseconds: the required milliseconds. |
| 3037 | |
| 3038 | This function may be used when a lua action returns `act.YIELD`, to force its |
| 3039 | wake-up at most after the specified number of milliseconds. |
| 3040 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 3041 | External Lua libraries |
| 3042 | ====================== |
| 3043 | |
| 3044 | A lot of useful lua libraries can be found here: |
| 3045 | |
| 3046 | * `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_ |
| 3047 | |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 3048 | Redis client library: |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 3049 | |
| 3050 | * `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_ |
| 3051 | |
| 3052 | This is an example about the usage of the Redis library with HAProxy. Note that |
| 3053 | each call of any function of this library can throw an error if the socket |
| 3054 | connection fails. |
| 3055 | |
| 3056 | .. code-block:: lua |
| 3057 | |
| 3058 | -- load the redis library |
| 3059 | local redis = require("redis"); |
| 3060 | |
| 3061 | function do_something(txn) |
| 3062 | |
| 3063 | -- create and connect new tcp socket |
| 3064 | local tcp = core.tcp(); |
| 3065 | tcp:settimeout(1); |
| 3066 | tcp:connect("127.0.0.1", 6379); |
| 3067 | |
| 3068 | -- use the redis library with this new socket |
| 3069 | local client = redis.connect({socket=tcp}); |
| 3070 | client:ping(); |
| 3071 | |
| 3072 | end |
| 3073 | |
| 3074 | OpenSSL: |
| 3075 | |
| 3076 | * `http://mkottman.github.io/luacrypto/index.html |
| 3077 | <http://mkottman.github.io/luacrypto/index.html>`_ |
| 3078 | |
| 3079 | * `https://github.com/brunoos/luasec/wiki |
| 3080 | <https://github.com/brunoos/luasec/wiki>`_ |