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