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