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