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