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 | |
| 30 | 2. The Lua **init context**. It is an Lua function executed just after the |
| 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 | |
| 38 | 3. The Lua **task context**. It is an Lua function executed after the start |
| 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 | |
| 45 | 4. The **action context**. It is an Lua function conditionally executed. These |
| 46 | actions are declared by the HAProxy directives "`tcp-request content lua |
| 47 | <function>`", "`tcp-response content lua <function>`", "`http-request lua |
| 48 | <function>`" and "`http-response lua <function>`". The prototype of the |
| 49 | Lua called function is a function with doesn't returns anything and that take |
| 50 | an object of class TXN as entry. `function fcn(txn)` |
| 51 | |
| 52 | 5. The **sample-fetch context**. This function takes a TXN object as entry |
| 53 | argument and returns a string. These types of function cannot execute any |
| 54 | blocking function. They are useful to aggregate some of original HAProxy |
| 55 | sample-fetches and return the result. The prototype of the function is |
| 56 | `function string fcn(txn)`. These functions can be registered with the Lua |
| 57 | function `core.register_fetches()`. Each declared sample-fetch is prefixed by |
| 58 | the string "lua.". |
| 59 | |
| 60 | **NOTE**: It is possible that this function cannot found the required data |
| 61 | in the original HAProxy sample-fetches, in this case, it cannot return the |
| 62 | result. This case is not yet supported |
| 63 | |
| 64 | 6. The **converter context**. It is an Lua function that takes a string as input |
| 65 | and returns another string as output. These types of function are stateless, |
| 66 | it cannot access to any context. They don't execute any blocking function. |
| 67 | The call prototype is `function string fcn(string)`. This function can be |
| 68 | registered with the Lua function `core.register_converters()`. Each declared |
| 69 | converter is prefixed by the string "lua.". |
| 70 | |
| 71 | HAProxy Lua Hello world |
| 72 | ----------------------- |
| 73 | |
| 74 | HAProxy configuration file (`hello_world.conf`): |
| 75 | |
| 76 | :: |
| 77 | |
| 78 | global |
| 79 | lua-load hello_world.lua |
| 80 | |
| 81 | listen proxy |
| 82 | bind 127.0.0.1:10001 |
| 83 | tcp-request content lua hello_world |
| 84 | |
| 85 | HAProxy Lua file (`hello_world.lua`): |
| 86 | |
| 87 | .. code-block:: lua |
| 88 | |
| 89 | function hello_world(txn) |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 90 | txn.res:send("hello world\n") |
| 91 | txn:close() |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 92 | end |
| 93 | |
| 94 | How to start HAProxy for testing this configuration: |
| 95 | |
| 96 | :: |
| 97 | |
| 98 | ./haproxy -f hello_world.conf |
| 99 | |
| 100 | On other terminal, you can test with telnet: |
| 101 | |
| 102 | :: |
| 103 | |
| 104 | #:~ telnet 127.0.0.1 10001 |
| 105 | hello world |
| 106 | |
| 107 | Core class |
| 108 | ========== |
| 109 | |
| 110 | .. js:class:: core |
| 111 | |
| 112 | The "core" class contains all the HAProxy core functions. These function are |
| 113 | useful for the controlling the execution flow, registering hooks, manipulating |
| 114 | global maps or ACL, ... |
| 115 | |
| 116 | "core" class is basically provided with HAProxy. No `require` line is |
| 117 | required to uses these function. |
| 118 | |
| 119 | The "core" class is static, t is not possible to create a new object of this |
| 120 | type. |
| 121 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 122 | .. js:attribute:: core.emerg |
| 123 | |
| 124 | This attribute is an integer, it contains the value of the loglevel "emergency" (0). |
| 125 | |
| 126 | .. js:attribute:: core.alert |
| 127 | |
| 128 | This attribute is an integer, it contains the value of the loglevel "alert" (1). |
| 129 | |
| 130 | .. js:attribute:: core.crit |
| 131 | |
| 132 | This attribute is an integer, it contains the value of the loglevel "critical" (2). |
| 133 | |
| 134 | .. js:attribute:: core.err |
| 135 | |
| 136 | This attribute is an integer, it contains the value of the loglevel "error" (3). |
| 137 | |
| 138 | .. js:attribute:: core.warning |
| 139 | |
| 140 | This attribute is an integer, it contains the value of the loglevel "warning" (4). |
| 141 | |
| 142 | .. js:attribute:: core.notice |
| 143 | |
| 144 | This attribute is an integer, it contains the value of the loglevel "notice" (5). |
| 145 | |
| 146 | .. js:attribute:: core.info |
| 147 | |
| 148 | This attribute is an integer, it contains the value of the loglevel "info" (6). |
| 149 | |
| 150 | .. js:attribute:: core.debug |
| 151 | |
| 152 | This attribute is an integer, it contains the value of the loglevel "debug" (7). |
| 153 | |
| 154 | .. js:function:: core.log(loglevel, msg) |
| 155 | |
| 156 | **context**: body, init, task, action, sample-fetch, converter |
| 157 | |
| 158 | This fucntion sends a log. The log is sent, according with the HAProxy |
| 159 | configuration file, on the default syslog server if it is configured and on |
| 160 | the stderr if it is allowed. |
| 161 | |
| 162 | :param integer loglevel: Is the log level asociated with the message. It is a |
| 163 | number between 0 and 7. |
| 164 | :param string msg: The log content. |
| 165 | :see: core.emerg, core.alert, core.crit, core.err, core.warning, core.notice, |
| 166 | core.info, core.debug (log level definitions) |
| 167 | :see: code.Debug |
| 168 | :see: core.Info |
| 169 | :see: core.Warning |
| 170 | :see: core.Alert |
| 171 | |
| 172 | .. js:function:: core.Debug(msg) |
| 173 | |
| 174 | **context**: body, init, task, action, sample-fetch, converter |
| 175 | |
| 176 | :param string msg: The log content. |
| 177 | :see: log |
| 178 | |
| 179 | Does the same job than: |
| 180 | |
| 181 | .. code-block:: lua |
| 182 | |
| 183 | function Debug(msg) |
| 184 | core.log(core.debug, msg) |
| 185 | end |
| 186 | .. |
| 187 | |
| 188 | .. js:function:: core.Info(msg) |
| 189 | |
| 190 | **context**: body, init, task, action, sample-fetch, converter |
| 191 | |
| 192 | :param string msg: The log content. |
| 193 | :see: log |
| 194 | |
| 195 | .. code-block:: lua |
| 196 | |
| 197 | function Info(msg) |
| 198 | core.log(core.info, msg) |
| 199 | end |
| 200 | .. |
| 201 | |
| 202 | .. js:function:: core.Warning(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 | |
| 211 | function Warning(msg) |
| 212 | core.log(core.warning, msg) |
| 213 | end |
| 214 | .. |
| 215 | |
| 216 | .. js:function:: core.Alert(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 | |
| 225 | function Alert(msg) |
| 226 | core.log(core.alert, msg) |
| 227 | end |
| 228 | .. |
| 229 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 230 | .. js:function:: core.add_acl(filename, key) |
| 231 | |
| 232 | **context**: init, task, action, sample-fetch, converter |
| 233 | |
| 234 | Add the ACL *key* in the ACLs list referenced by the file *filename*. |
| 235 | |
| 236 | :param string filename: the filename that reference the ACL entries. |
| 237 | :param string key: the key which will be added. |
| 238 | |
| 239 | .. js:function:: core.del_acl(filename, key) |
| 240 | |
| 241 | **context**: init, task, action, sample-fetch, converter |
| 242 | |
| 243 | Delete the ACL entry referenced by the key *key* in the list of ACLs |
| 244 | referenced by *filename*. |
| 245 | |
| 246 | :param string filename: the filename that reference the ACL entries. |
| 247 | :param string key: the key which will be deleted. |
| 248 | |
| 249 | .. js:function:: core.del_map(filename, key) |
| 250 | |
| 251 | **context**: init, task, action, sample-fetch, converter |
| 252 | |
| 253 | Delete the map entry indexed with the specified key in the list of maps |
| 254 | referenced by his filename. |
| 255 | |
| 256 | :param string filename: the filename that reference the map entries. |
| 257 | :param string key: the key which will be deleted. |
| 258 | |
| 259 | .. js:function:: core.msleep(milliseconds) |
| 260 | |
| 261 | **context**: body, init, task, action |
| 262 | |
| 263 | The `core.msleep()` stops the Lua execution between specified milliseconds. |
| 264 | |
| 265 | :param integer milliseconds: the required milliseconds. |
| 266 | |
| 267 | .. js:function:: core.register_converters(name, func) |
| 268 | |
| 269 | **context**: body |
| 270 | |
| 271 | Register an Lua function executed as converter. All the registered converters |
| 272 | can be used in HAProxy with the prefix "lua.". An converter get a string as |
| 273 | input and return a string as output. The registered function can take up to 9 |
| 274 | values as parameter. All the value are strings. |
| 275 | |
| 276 | :param string name: is the name of the converter. |
| 277 | :param function func: is the Lua function called to work as converter. |
| 278 | |
| 279 | The prototype of the Lua function used as argument is: |
| 280 | |
| 281 | .. code-block:: lua |
| 282 | |
| 283 | function(str, [p1 [, p2 [, ... [, p5]]]]) |
| 284 | .. |
| 285 | |
| 286 | * **str** (*string*): this is the input value automatically converted in |
| 287 | string. |
| 288 | * **p1** .. **p5** (*string*): this is a list of string arguments declared in |
| 289 | the haroxy configuration file. The number of arguments doesn't exceed 5. |
| 290 | The order and the nature of these is conventionally choose by the |
| 291 | developper. |
| 292 | |
| 293 | .. js:function:: core.register_fetches(name, func) |
| 294 | |
| 295 | **context**: body |
| 296 | |
| 297 | Register an Lua function executed as sample fetch. All the registered sample |
| 298 | fetchs can be used in HAProxy with the prefix "lua.". A Lua sample fetch |
| 299 | return a string as output. The registered function can take up to 9 values as |
| 300 | parameter. All the value are strings. |
| 301 | |
| 302 | :param string name: is the name of the converter. |
| 303 | :param function func: is the Lua function called to work as sample fetch. |
| 304 | |
| 305 | The prototype of the Lua function used as argument is: |
| 306 | |
| 307 | .. code-block:: lua |
| 308 | |
| 309 | string function(txn, [p1 [, p2 [, ... [, p5]]]]) |
| 310 | .. |
| 311 | |
| 312 | * **txn** (*class txn*): this is the txn object associated with the current |
| 313 | request. |
| 314 | * **p1** .. **p5** (*string*): this is a list of string arguments declared in |
| 315 | the haroxy configuration file. The number of arguments doesn't exceed 5. |
| 316 | The order and the nature of these is conventionally choose by the |
| 317 | developper. |
| 318 | * **Returns**: A string containing some data, ot nil if the value cannot be |
| 319 | returned now. |
| 320 | |
| 321 | lua example code: |
| 322 | |
| 323 | .. code-block:: lua |
| 324 | |
| 325 | core.register_fetches("hello", function(txn) |
| 326 | return "hello" |
| 327 | end) |
| 328 | .. |
| 329 | |
| 330 | HAProxy example configuration: |
| 331 | |
| 332 | :: |
| 333 | |
| 334 | frontend example |
| 335 | http-request redirect location /%[lua.hello] |
| 336 | |
| 337 | .. js:function:: core.register_init(func) |
| 338 | |
| 339 | **context**: body |
| 340 | |
| 341 | Register a function executed after the configuration parsing. This is useful |
| 342 | to check any parameters. |
| 343 | |
| 344 | :param fuction func: is the Lua function called to work as initializer. |
| 345 | |
| 346 | The prototype of the Lua function used as argument is: |
| 347 | |
| 348 | .. code-block:: lua |
| 349 | |
| 350 | function() |
| 351 | .. |
| 352 | |
| 353 | It takes no input, and no output is expected. |
| 354 | |
| 355 | .. js:function:: core.register_task(func) |
| 356 | |
| 357 | **context**: body, init, task, action, sample-fetch, converter |
| 358 | |
| 359 | Register and start independent task. The task is started when the HAProxy |
| 360 | main scheduler starts. For example this type of tasks can be executed to |
| 361 | perform complex health checks. |
| 362 | |
| 363 | :param fuction func: is the Lua function called to work as initializer. |
| 364 | |
| 365 | The prototype of the Lua function used as argument is: |
| 366 | |
| 367 | .. code-block:: lua |
| 368 | |
| 369 | function() |
| 370 | .. |
| 371 | |
| 372 | It takes no input, and no output is expected. |
| 373 | |
| 374 | .. js:function:: core.set_nice(nice) |
| 375 | |
| 376 | **context**: task, action, sample-fetch, converter |
| 377 | |
| 378 | Change the nice of the current task or current session. |
| 379 | |
| 380 | :param integer nice: the nice value, it must be between -1024 and 1024. |
| 381 | |
| 382 | .. js:function:: core.set_map(filename, key, value) |
| 383 | |
| 384 | **context**: init, task, action, sample-fetch, converter |
| 385 | |
| 386 | set the value *value* associated to the key *key* in the map referenced by |
| 387 | *filename*. |
| 388 | |
| 389 | .. js:function:: core.sleep(int seconds) |
| 390 | |
| 391 | **context**: body, init, task, action |
| 392 | |
| 393 | The `core.sleep()` functions stop the Lua execution between specified seconds. |
| 394 | |
| 395 | :param integer seconds: the required seconds. |
| 396 | |
| 397 | .. js:function:: core.tcp() |
| 398 | |
| 399 | **context**: init, task, action |
| 400 | |
| 401 | This function returns a new object of a *socket* class. |
| 402 | |
| 403 | :returns: A socket class object. |
| 404 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 405 | .. js:function:: core.yield() |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 406 | |
| 407 | **context**: task, action, sample-fetch, converter |
| 408 | |
| 409 | Give back the hand at the HAProxy scheduler. It is used when the LUA |
| 410 | processing consumes a lot of processing time. |
| 411 | |
| 412 | Fetches class |
| 413 | ============= |
| 414 | |
| 415 | .. js:class:: Fetches |
| 416 | |
| 417 | This class contains a lot of internal HAProxy sample fetches. See the |
| 418 | HAProxy documentation for more information about her usage. |
| 419 | |
| 420 | Converters class |
| 421 | ================ |
| 422 | |
| 423 | .. js:class:: Converters |
| 424 | |
| 425 | This class contains a lot of internal HAProxy sample converters. See the |
| 426 | HAProxy documentation for more information about her usage. |
| 427 | |
| 428 | Channel class |
| 429 | ============= |
| 430 | |
| 431 | .. js:class:: Channel |
| 432 | |
| 433 | HAProxy uses two buffers for the processing of the requests. The first one is |
| 434 | used with the request data (from the client to the server) and the second is |
| 435 | used for the response data (from the server to the client). |
| 436 | |
| 437 | Each buffer contains two types of data. The first type is the incoming data |
| 438 | waiting for a processing. The second part is the outgoing data already |
| 439 | processed. Usually, the incoming data is processed, after it is tagged as |
| 440 | outgoing data, and finally it is sent. The following functions provides tools |
| 441 | for manipulating these data in a buffer. |
| 442 | |
| 443 | The following diagram shows where the channel class function are applied. |
| 444 | |
| 445 | **Warning**: It is not possible to read from the response in request action, |
| 446 | and it is not possible to read for the request channel in response action. |
| 447 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 448 | .. image:: _static/Channel.png |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 449 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 450 | .. js:function:: Channel.dup(channel) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 451 | |
| 452 | This function returns a string that contain the entire buffer. The data is |
| 453 | not remove from the buffer and can be reprocessed later. |
| 454 | |
| 455 | If the buffer cant receive more data, a 'nil' value is returned. |
| 456 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 457 | :param class_channel channel: The manipulated Channel. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 458 | :returns: a string containig all the avalaible data or nil. |
| 459 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 460 | .. js:function:: Channel.get(channel) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 461 | |
| 462 | This function returns a string that contain the entire buffer. The data is |
| 463 | consumed from the buffer. |
| 464 | |
| 465 | If the buffer cant receive more data, a 'nil' value is returned. |
| 466 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 467 | :param class_channel channel: The manipulated Channel. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 468 | :returns: a string containig all the avalaible data or nil. |
| 469 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 470 | .. js:function:: Channel.get_line(channel) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 471 | |
| 472 | This function returns a string that contain the first line of the buffer. The |
| 473 | data is consumed. If the data returned doesn't contains a final '\n' its |
| 474 | assumed than its the last available data in the buffer. |
| 475 | |
| 476 | If the buffer cant receive more data, a 'nil' value is returned. |
| 477 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 478 | :param class_channel channel: The manipulated Channel. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 479 | :returns: a string containig the avalaiable line or nil. |
| 480 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 481 | .. js:function:: Channel.set(channel, string) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 482 | |
| 483 | This function replace the content of the buffer by the string. The function |
| 484 | returns the copied length, otherwise, it returns -1. |
| 485 | |
| 486 | The data set with this function are not send. They wait for the end of |
| 487 | HAProxy processing, so the buffer can be full. |
| 488 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 489 | :param class_channel channel: The manipulated Channel. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 490 | :param string string: The data which will sent. |
| 491 | :returns: an integer containing the amount of butes copyed or -1. |
| 492 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 493 | .. js:function:: Channel.append(channel, string) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 494 | |
| 495 | This function append the string argument to the content of the buffer. The |
| 496 | function returns the copied length, otherwise, it returns -1. |
| 497 | |
| 498 | The data set with this function are not send. They wait for the end of |
| 499 | HAProxy processing, so the buffer can be full. |
| 500 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 501 | :param class_channel channel: The manipulated Channel. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 502 | :param string string: The data which will sent. |
| 503 | :returns: an integer containing the amount of butes copyed or -1. |
| 504 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 505 | .. js:function:: Channel.send(channel, string) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 506 | |
| 507 | This function required immediate send of the data. Unless if the connection |
| 508 | is close, the buffer is regularly flushed and all the string can be sent. |
| 509 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 510 | :param class_channel channel: The manipulated Channel. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 511 | :param string string: The data which will sent. |
| 512 | :returns: an integer containing the amount of butes copyed or -1. |
| 513 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 514 | .. js:function:: Channel.get_in_length(channel) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 515 | |
| 516 | This function returns the length of the input part of the buffer. |
| 517 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 518 | :param class_channel channel: The manipulated Channel. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 519 | :returns: an integer containing the amount of avalaible bytes. |
| 520 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 521 | .. js:function:: Channel.get_out_length(channel) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 522 | |
| 523 | This function returns the length of the output part of the buffer. |
| 524 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 525 | :param class_channel channel: The manipulated Channel. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 526 | :returns: an integer containing the amount of avalaible bytes. |
| 527 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 528 | .. js:function:: Channel.forward(channel, int) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 529 | |
| 530 | This function transfer bytes from the input part of the buffer to the output |
| 531 | part. |
| 532 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 533 | :param class_channel channel: The manipulated Channel. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 534 | :param integer int: The amount of data which will be forwarded. |
| 535 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 536 | |
| 537 | HTTP class |
| 538 | ========== |
| 539 | |
| 540 | .. js:class:: HTTP |
| 541 | |
| 542 | This class contain all the HTTP manipulation functions. |
| 543 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 544 | .. js:function:: HTTP.req_get_header(http) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 545 | |
| 546 | Returns an array containing all the request headers. |
| 547 | |
| 548 | :param class_http http: The related http object. |
| 549 | :returns: array of headers. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 550 | :see: HTTP.res_get_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 551 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 552 | .. js:function:: HTTP.res_get_header(http) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 553 | |
| 554 | Returns an array containing all the response headers. |
| 555 | |
| 556 | :param class_http http: The related http object. |
| 557 | :returns: array of headers. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 558 | :see: HTTP.req_get_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 559 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 560 | .. js:function:: HTTP.req_add_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 561 | |
| 562 | Appends an HTTP header field in the request whose name is |
| 563 | specified in "name" and whose value is defined in "value". |
| 564 | |
| 565 | :param class_http http: The related http object. |
| 566 | :param string name: The header name. |
| 567 | :param string value: The header value. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 568 | :see: HTTP.res_add_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 569 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 570 | .. js:function:: HTTP.res_add_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 571 | |
| 572 | appends an HTTP header field in the response whose name is |
| 573 | specified in "name" and whose value is defined in "value". |
| 574 | |
| 575 | :param class_http http: The related http object. |
| 576 | :param string name: The header name. |
| 577 | :param string value: The header value. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 578 | :see: HTTP.req_add_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 579 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 580 | .. js:function:: HTTP.req_del_header(http, name) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 581 | |
| 582 | Removes all HTTP header fields in the request whose name is |
| 583 | specified in "name". |
| 584 | |
| 585 | :param class_http http: The related http object. |
| 586 | :param string name: The header name. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 587 | :see: HTTP.res_del_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 588 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 589 | .. js:function:: HTTP.res_del_header(http, name) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 590 | |
| 591 | Removes all HTTP header fields in the response whose name is |
| 592 | specified in "name". |
| 593 | |
| 594 | :param class_http http: The related http object. |
| 595 | :param string name: The header name. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 596 | :see: HTTP.req_del_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 597 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 598 | .. js:function:: HTTP.req_set_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 599 | |
| 600 | This variable replace all occurence of all header "name", by only |
| 601 | one containing the "value". |
| 602 | |
| 603 | :param class_http http: The related http object. |
| 604 | :param string name: The header name. |
| 605 | :param string value: The header value. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 606 | :see: HTTP.res_set_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 607 | |
| 608 | This function does the same work as the folowwing code: |
| 609 | |
| 610 | .. code-block:: lua |
| 611 | |
| 612 | function fcn(txn) |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 613 | TXN.http:req_del_header("header") |
| 614 | TXN.http:req_add_header("header", "value") |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 615 | end |
| 616 | .. |
| 617 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 618 | .. js:function:: HTTP.res_set_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 619 | |
| 620 | This variable replace all occurence of all header "name", by only |
| 621 | one containing the "value". |
| 622 | |
| 623 | :param class_http http: The related http object. |
| 624 | :param string name: The header name. |
| 625 | :param string value: The header value. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 626 | :see: HTTP.req_set_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 627 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 628 | .. js:function:: HTTP.req_replace_header(http, name, regex, replace) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 629 | |
| 630 | Matches the regular expression in all occurrences of header field "name" |
| 631 | according to "regex", and replaces them with the "replace" argument. The |
| 632 | replacement value can contain back references like \1, \2, ... This |
| 633 | function works with the request. |
| 634 | |
| 635 | :param class_http http: The related http object. |
| 636 | :param string name: The header name. |
| 637 | :param string regex: The match regular expression. |
| 638 | :param string replace: The replacement value. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 639 | :see: HTTP.res_replace_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 640 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 641 | .. js:function:: HTTP.res_replace_header(http, name, regex, string) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 642 | |
| 643 | Matches the regular expression in all occurrences of header field "name" |
| 644 | according to "regex", and replaces them with the "replace" argument. The |
| 645 | replacement value can contain back references like \1, \2, ... This |
| 646 | function works with the request. |
| 647 | |
| 648 | :param class_http http: The related http object. |
| 649 | :param string name: The header name. |
| 650 | :param string regex: The match regular expression. |
| 651 | :param string replace: The replacement value. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 652 | :see: HTTP.req_replace_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 653 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 654 | .. js:function:: HTTP.req_replace_value(http, name, regex, replace) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 655 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 656 | Works like "HTTP.req_replace_header()" except that it matches the regex |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 657 | against every comma-delimited value of the header field "name" instead of the |
| 658 | entire header. |
| 659 | |
| 660 | :param class_http http: The related http object. |
| 661 | :param string name: The header name. |
| 662 | :param string regex: The match regular expression. |
| 663 | :param string replace: The replacement value. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 664 | :see: HTTP.req_replace_header() |
| 665 | :see: HTTP.res_replace_value() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 666 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 667 | .. js:function:: HTTP.res_replace_value(http, name, regex, replace) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 668 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 669 | Works like "HTTP.res_replace_header()" except that it matches the regex |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 670 | against every comma-delimited value of the header field "name" instead of the |
| 671 | entire header. |
| 672 | |
| 673 | :param class_http http: The related http object. |
| 674 | :param string name: The header name. |
| 675 | :param string regex: The match regular expression. |
| 676 | :param string replace: The replacement value. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 677 | :see: HTTP.res_replace_header() |
| 678 | :see: HTTP.req_replace_value() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 679 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 680 | .. js:function:: HTTP.req_set_method(http, method) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 681 | |
| 682 | Rewrites the request method with the parameter "method". |
| 683 | |
| 684 | :param class_http http: The related http object. |
| 685 | :param string method: The new method. |
| 686 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 687 | .. js:function:: HTTP.req_set_path(http, path) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 688 | |
| 689 | Rewrites the request path with the "path" parameter. |
| 690 | |
| 691 | :param class_http http: The related http object. |
| 692 | :param string path: The new path. |
| 693 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 694 | .. js:function:: HTTP.req_set_query(http, query) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 695 | |
| 696 | Rewrites the request's query string which appears after the first question |
| 697 | mark ("?") with the parameter "query". |
| 698 | |
| 699 | :param class_http http: The related http object. |
| 700 | :param string query: The new query. |
| 701 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 702 | .. js:function:: HTTP.req.set_uri(http, uri) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 703 | |
| 704 | Rewrites the request URI with the parameter "uri". |
| 705 | |
| 706 | :param class_http http: The related http object. |
| 707 | :param string uri: The new uri. |
| 708 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 709 | TXN class |
| 710 | ========= |
| 711 | |
| 712 | .. js:class:: TXN |
| 713 | |
| 714 | The txn class contain all the functions relative to the http or tcp |
| 715 | transaction (Note than a tcp stream is the same than a tcp transaction, but |
| 716 | an HTTP transaction is not the same than a tcp stream). |
| 717 | |
| 718 | The usage of this class permits to retrieve data from the requests, alter it |
| 719 | and forward it. |
| 720 | |
| 721 | All the functions provided by this class are available in the context |
| 722 | **sample-fetches** and **actions**. |
| 723 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 724 | .. js:attribute:: TXN.c |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 725 | |
| 726 | This attribute contains a Converters class object. |
| 727 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 728 | .. js:attribute:: TXN.sc |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 729 | |
| 730 | This attribute contains a Converters class object. The functions of |
| 731 | this object returns always a string. |
| 732 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 733 | .. js:attribute:: TXN.f |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 734 | |
| 735 | This attribute contains a Fetches class object. |
| 736 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 737 | .. js:attribute:: TXN.sf |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 738 | |
| 739 | This attribute contains a Fetches class object. The functions of |
| 740 | this object returns always a string. |
| 741 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 742 | .. js:attribute:: TXN.req |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 743 | |
| 744 | This attribute contains a channel class object for the request buffer. |
| 745 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 746 | .. js:attribute:: TXN.res |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 747 | |
| 748 | This attribute contains a channel class object for the response buffer. |
| 749 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 750 | .. js:attribute:: TXN.http |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 751 | |
| 752 | This attribute contains an HTTP class object. It is avalaible only if the |
| 753 | proxy has the "mode http" enabled. |
| 754 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 755 | .. js:function:: TXN.log(TXN, loglevel, msg) |
| 756 | |
| 757 | This function sends a log. The log is sent, according with the HAProxy |
| 758 | configuration file, on the default syslog server if it is configured and on |
| 759 | the stderr if it is allowed. |
| 760 | |
| 761 | :param class_txn txn: The class txn object containing the data. |
| 762 | :param integer loglevel: Is the log level asociated with the message. It is a |
| 763 | number between 0 and 7. |
| 764 | :param string msg: The log content. |
| 765 | :see: core.emerg, core.alert, core.crit, core.err, core.warning, core.notice, |
| 766 | core.info, core.debug (log level definitions) |
| 767 | :see: TXN.deflog |
| 768 | :see: TXN.Debug |
| 769 | :see: TXN.Info |
| 770 | :see: TXN.Warning |
| 771 | :see: TXN.Alert |
| 772 | |
| 773 | .. js:function:: TXN.deflog(TXN, msg) |
| 774 | |
| 775 | Sends a log line with the default loglevel for the proxy ssociated with the |
| 776 | transaction. |
| 777 | |
| 778 | :param class_txn txn: The class txn object containing the data. |
| 779 | :param string msg: The log content. |
| 780 | :see: TXN.log |
| 781 | |
| 782 | .. js:function:: TXN.Debug(txn, msg) |
| 783 | |
| 784 | :param class_txn txn: The class txn object containing the data. |
| 785 | :param string msg: The log content. |
| 786 | :see: TXN.log |
| 787 | |
| 788 | Does the same job than: |
| 789 | |
| 790 | .. code-block:: lua |
| 791 | |
| 792 | function Debug(txn, msg) |
| 793 | TXN.log(txn, core.debug, msg) |
| 794 | end |
| 795 | .. |
| 796 | |
| 797 | .. js:function:: TXN.Info(txn, msg) |
| 798 | |
| 799 | :param class_txn txn: The class txn object containing the data. |
| 800 | :param string msg: The log content. |
| 801 | :see: TXN.log |
| 802 | |
| 803 | .. code-block:: lua |
| 804 | |
| 805 | function Debug(txn, msg) |
| 806 | TXN.log(txn, core.info, msg) |
| 807 | end |
| 808 | .. |
| 809 | |
| 810 | .. js:function:: TXN.Warning(txn, msg) |
| 811 | |
| 812 | :param class_txn txn: The class txn object containing the data. |
| 813 | :param string msg: The log content. |
| 814 | :see: TXN.log |
| 815 | |
| 816 | .. code-block:: lua |
| 817 | |
| 818 | function Debug(txn, msg) |
| 819 | TXN.log(txn, core.warning, msg) |
| 820 | end |
| 821 | .. |
| 822 | |
| 823 | .. js:function:: TXN.Alert(txn, msg) |
| 824 | |
| 825 | :param class_txn txn: The class txn object containing the data. |
| 826 | :param string msg: The log content. |
| 827 | :see: TXN.log |
| 828 | |
| 829 | .. code-block:: lua |
| 830 | |
| 831 | function Debug(txn, msg) |
| 832 | TXN.log(txn, core.alert, msg) |
| 833 | end |
| 834 | .. |
| 835 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 836 | .. js:function:: TXN.get_priv(txn) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 837 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 838 | 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] | 839 | function. If no data are stored, it returns a nil value. |
| 840 | |
| 841 | :param class_txn txn: The class txn object containing the data. |
| 842 | :returns: the opaque data previsously stored, or nil if nothing is |
| 843 | avalaible. |
| 844 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 845 | .. js:function:: TXN.set_priv(txn, data) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 846 | |
| 847 | Store any data in the current HAProxy transaction. This action replace the |
| 848 | old stored data. |
| 849 | |
| 850 | :param class_txn txn: The class txn object containing the data. |
| 851 | :param opaque data: The data which is stored in the transaction. |
| 852 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 853 | .. js:function:: TXN.get_headers(txn) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 854 | |
| 855 | This function returns an array of headers. |
| 856 | |
| 857 | :param class_txn txn: The class txn object containing the data. |
| 858 | :returns: an array of headers. |
| 859 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 860 | .. js:function:: TXN.close(txn) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 861 | |
| 862 | This function close the transaction and the associated session. It can be |
| 863 | used when a critical error is detected. |
| 864 | |
| 865 | :param class_txn txn: The class txn object containing the data. |
| 866 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 867 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 868 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 869 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 870 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 871 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 872 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 873 | .. js:function:: TXN.set_loglevel(txn, loglevel) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 874 | |
| 875 | Is used to change the log level of the current request. The "loglevel" must |
| 876 | be an integer between 0 and 7. |
| 877 | |
| 878 | :param class_txn txn: The class txn object containing the data. |
| 879 | :param integer loglevel: The required log level. This variable can be one of |
| 880 | :see: core.<loglevel> |
| 881 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 882 | .. js:function:: TXN.set_tos(txn, tos) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 883 | |
| 884 | Is used to set the TOS or DSCP field value of packets sent to the client to |
| 885 | the value passed in "tos" on platforms which support this. |
| 886 | |
| 887 | :param class_txn txn: The class txn object containing the data. |
| 888 | :param integer tos: The new TOS os DSCP. |
| 889 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 890 | .. js:function:: TXN.set_mark(txn, mark) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 891 | |
| 892 | Is used to set the Netfilter MARK on all packets sent to the client to the |
| 893 | value passed in "mark" on platforms which support it. |
| 894 | |
| 895 | :param class_txn txn: The class txn object containing the data. |
| 896 | :param integer mark: The mark value. |
| 897 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 898 | Socket class |
| 899 | ============ |
| 900 | |
| 901 | .. js:class:: Socket |
| 902 | |
| 903 | This class must be compatible with the Lua Socket class. Only the 'client' |
| 904 | functions are available. See the Lua Socket documentation: |
| 905 | |
| 906 | `http://w3.impa.br/~diego/software/luasocket/tcp.html |
| 907 | <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_ |
| 908 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 909 | .. js:function:: Socket.close(socket) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 910 | |
| 911 | Closes a TCP object. The internal socket used by the object is closed and the |
| 912 | local address to which the object was bound is made available to other |
| 913 | applications. No further operations (except for further calls to the close |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 914 | method) are allowed on a closed Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 915 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 916 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 917 | |
| 918 | Note: It is important to close all used sockets once they are not needed, |
| 919 | since, in many systems, each socket uses a file descriptor, which are limited |
| 920 | system resources. Garbage-collected objects are automatically closed before |
| 921 | destruction, though. |
| 922 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 923 | .. js:function:: Socket.connect(socket, address, port) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 924 | |
| 925 | Attempts to connect a socket object to a remote host. |
| 926 | |
| 927 | Address can be an IP address or a host name. Port must be an integer number |
| 928 | in the range [1..64K). |
| 929 | |
| 930 | In case of error, the method returns nil followed by a string describing the |
| 931 | error. In case of success, the method returns 1. |
| 932 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 933 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 934 | :returns: 1 or nil. |
| 935 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 936 | Note: The function Socket.connect is available and is a shortcut for the |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 937 | creation of client sockets. |
| 938 | |
| 939 | Note: Starting with LuaSocket 2.0, the settimeout method affects the behavior |
| 940 | of connect, causing it to return with an error in case of a timeout. If that |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 941 | happens, you can still call Socket.select with the socket in the sendt table. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 942 | The socket will be writable when the connection is established. |
| 943 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 944 | .. js:function:: Socket.connect_ssl(socket, address, port) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 945 | |
| 946 | Same behavior than the function socket:connect, but uses SSL. |
| 947 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 948 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 949 | :returns: 1 or nil. |
| 950 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 951 | .. js:function:: Socket.getpeername(socket) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 952 | |
| 953 | Returns information about the remote side of a connected client object. |
| 954 | |
| 955 | Returns a string with the IP address of the peer, followed by the port number |
| 956 | that peer is using for the connection. In case of error, the method returns |
| 957 | nil. |
| 958 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 959 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 960 | :returns: a string containing the server information. |
| 961 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 962 | .. js:function:: Socket.getsockname(socket) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 963 | |
| 964 | Returns the local address information associated to the object. |
| 965 | |
| 966 | The method returns a string with local IP address and a number with the port. |
| 967 | In case of error, the method returns nil. |
| 968 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 969 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 970 | :returns: a string containing the client information. |
| 971 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 972 | .. js:function:: Socket.receive(socket, [pattern [, prefix]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 973 | |
| 974 | Reads data from a client object, according to the specified read pattern. |
| 975 | Patterns follow the Lua file I/O format, and the difference in performance |
| 976 | between all patterns is negligible. |
| 977 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 978 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 979 | :param string|integer pattern: Describe what is required (see below). |
| 980 | :param string prefix: A string which will be prefix the returned data. |
| 981 | :returns: a string containing the required data or nil. |
| 982 | |
| 983 | Pattern can be any of the following: |
| 984 | |
| 985 | * **`*a`**: reads from the socket until the connection is closed. No |
| 986 | end-of-line translation is performed; |
| 987 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 988 | * **`*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] | 989 | LF character (ASCII 10), optionally preceded by a CR character |
| 990 | (ASCII 13). The CR and LF characters are not included in the |
| 991 | returned line. In fact, all CR characters are ignored by the |
| 992 | pattern. This is the default pattern. |
| 993 | |
| 994 | * **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] | 995 | Socket. Prefix is an optional string to be concatenated to the |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 996 | beginning of any received data before return. |
| 997 | |
| 998 | If successful, the method returns the received pattern. In case of error, the |
| 999 | method returns nil followed by an error message which can be the string |
| 1000 | 'closed' in case the connection was closed before the transmission was |
| 1001 | completed or the string 'timeout' in case there was a timeout during the |
| 1002 | operation. Also, after the error message, the function returns the partial |
| 1003 | result of the transmission. |
| 1004 | |
| 1005 | Important note: This function was changed severely. It used to support |
| 1006 | multiple patterns (but I have never seen this feature used) and now it |
| 1007 | doesn't anymore. Partial results used to be returned in the same way as |
| 1008 | successful results. This last feature violated the idea that all functions |
| 1009 | should return nil on error. Thus it was changed too. |
| 1010 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1011 | .. js:function:: Socket.send(socket, data [, start [, end ]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1012 | |
| 1013 | Sends data through client object. |
| 1014 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1015 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1016 | :param string data: The data that will be sent. |
| 1017 | :param integer start: The start position in the buffer of the data which will |
| 1018 | be sent. |
| 1019 | :param integer end: The end position in the buffer of the data which will |
| 1020 | be sent. |
| 1021 | :returns: see below. |
| 1022 | |
| 1023 | Data is the string to be sent. The optional arguments i and j work exactly |
| 1024 | like the standard string.sub Lua function to allow the selection of a |
| 1025 | substring to be sent. |
| 1026 | |
| 1027 | If successful, the method returns the index of the last byte within [start, |
| 1028 | end] that has been sent. Notice that, if start is 1 or absent, this is |
| 1029 | effectively the total number of bytes sent. In case of error, the method |
| 1030 | returns nil, followed by an error message, followed by the index of the last |
| 1031 | byte within [start, end] that has been sent. You might want to try again from |
| 1032 | the byte following that. The error message can be 'closed' in case the |
| 1033 | connection was closed before the transmission was completed or the string |
| 1034 | 'timeout' in case there was a timeout during the operation. |
| 1035 | |
| 1036 | Note: Output is not buffered. For small strings, it is always better to |
| 1037 | concatenate them in Lua (with the '..' operator) and send the result in one |
| 1038 | call instead of calling the method several times. |
| 1039 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1040 | .. js:function:: Socket.setoption(socket, option [, value]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1041 | |
| 1042 | Just implemented for compatibility, this cal does nothing. |
| 1043 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1044 | .. js:function:: Socket.settimeout(socket, value [, mode]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1045 | |
| 1046 | Changes the timeout values for the object. All I/O operations are blocking. |
| 1047 | That is, any call to the methods send, receive, and accept will block |
| 1048 | indefinitely, until the operation completes. The settimeout method defines a |
| 1049 | limit on the amount of time the I/O methods can block. When a timeout time |
| 1050 | has elapsed, the affected methods give up and fail with an error code. |
| 1051 | |
| 1052 | The amount of time to wait is specified as the value parameter, in seconds. |
| 1053 | |
| 1054 | The timeout modes are bot implemented, the only settable timeout is the |
| 1055 | inactivity time waiting for complete the internal buffer send or waiting for |
| 1056 | receive data. |
| 1057 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1058 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1059 | :param integer value: The timeout value. |
| 1060 | |
| 1061 | External Lua libraries |
| 1062 | ====================== |
| 1063 | |
| 1064 | A lot of useful lua libraries can be found here: |
| 1065 | |
| 1066 | * `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_ |
| 1067 | |
| 1068 | Redis acces: |
| 1069 | |
| 1070 | * `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_ |
| 1071 | |
| 1072 | This is an example about the usage of the Redis library with HAProxy. Note that |
| 1073 | each call of any function of this library can throw an error if the socket |
| 1074 | connection fails. |
| 1075 | |
| 1076 | .. code-block:: lua |
| 1077 | |
| 1078 | -- load the redis library |
| 1079 | local redis = require("redis"); |
| 1080 | |
| 1081 | function do_something(txn) |
| 1082 | |
| 1083 | -- create and connect new tcp socket |
| 1084 | local tcp = core.tcp(); |
| 1085 | tcp:settimeout(1); |
| 1086 | tcp:connect("127.0.0.1", 6379); |
| 1087 | |
| 1088 | -- use the redis library with this new socket |
| 1089 | local client = redis.connect({socket=tcp}); |
| 1090 | client:ping(); |
| 1091 | |
| 1092 | end |
| 1093 | |
| 1094 | OpenSSL: |
| 1095 | |
| 1096 | * `http://mkottman.github.io/luacrypto/index.html |
| 1097 | <http://mkottman.github.io/luacrypto/index.html>`_ |
| 1098 | |
| 1099 | * `https://github.com/brunoos/luasec/wiki |
| 1100 | <https://github.com/brunoos/luasec/wiki>`_ |
| 1101 | |