DOC: lua api

This contains the Lua API documentation and the build environment
for Sphinx.
This commit is contained in:
Thierry FOURNIER 2015-03-11 20:31:00 +01:00 committed by Willy Tarreau
parent 2694a1a3c8
commit 17bd152b58
5 changed files with 1208 additions and 0 deletions

153
doc/lua-api/Makefile Normal file
View File

@ -0,0 +1,153 @@
# Makefile for Sphinx documentation
#
# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = sphinx-build
PAPER =
BUILDDIR = _build
# Internal variables.
PAPEROPT_a4 = -D latex_paper_size=a4
PAPEROPT_letter = -D latex_paper_size=letter
ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
# the i18n builder cannot share the environment and doctrees with the others
I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext
help:
@echo "Please use \`make <target>' where <target> is one of"
@echo " html to make standalone HTML files"
@echo " dirhtml to make HTML files named index.html in directories"
@echo " singlehtml to make a single large HTML file"
@echo " pickle to make pickle files"
@echo " json to make JSON files"
@echo " htmlhelp to make HTML files and a HTML help project"
@echo " qthelp to make HTML files and a qthelp project"
@echo " devhelp to make HTML files and a Devhelp project"
@echo " epub to make an epub"
@echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
@echo " latexpdf to make LaTeX files and run them through pdflatex"
@echo " text to make text files"
@echo " man to make manual pages"
@echo " texinfo to make Texinfo files"
@echo " info to make Texinfo files and run them through makeinfo"
@echo " gettext to make PO message catalogs"
@echo " changes to make an overview of all changed/added/deprecated items"
@echo " linkcheck to check all external links for integrity"
@echo " doctest to run all doctests embedded in the documentation (if enabled)"
clean:
-rm -rf $(BUILDDIR)/*
html:
$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
@echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
dirhtml:
$(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml
@echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml."
singlehtml:
$(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml
@echo
@echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml."
pickle:
$(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle
@echo
@echo "Build finished; now you can process the pickle files."
json:
$(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json
@echo
@echo "Build finished; now you can process the JSON files."
htmlhelp:
$(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp
@echo
@echo "Build finished; now you can run HTML Help Workshop with the" \
".hhp project file in $(BUILDDIR)/htmlhelp."
qthelp:
$(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp
@echo
@echo "Build finished; now you can run "qcollectiongenerator" with the" \
".qhcp project file in $(BUILDDIR)/qthelp, like this:"
@echo "# qcollectiongenerator $(BUILDDIR)/qthelp/haproxy-lua.qhcp"
@echo "To view the help file:"
@echo "# assistant -collectionFile $(BUILDDIR)/qthelp/haproxy-lua.qhc"
devhelp:
$(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp
@echo
@echo "Build finished."
@echo "To view the help file:"
@echo "# mkdir -p $$HOME/.local/share/devhelp/haproxy-lua"
@echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/haproxy-lua"
@echo "# devhelp"
epub:
$(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub
@echo
@echo "Build finished. The epub file is in $(BUILDDIR)/epub."
latex:
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo
@echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex."
@echo "Run \`make' in that directory to run these through (pdf)latex" \
"(use \`make latexpdf' here to do that automatically)."
latexpdf:
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo "Running LaTeX files through pdflatex..."
$(MAKE) -C $(BUILDDIR)/latex all-pdf
@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
text:
$(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text
@echo
@echo "Build finished. The text files are in $(BUILDDIR)/text."
man:
$(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man
@echo
@echo "Build finished. The manual pages are in $(BUILDDIR)/man."
texinfo:
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
@echo
@echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo."
@echo "Run \`make' in that directory to run these through makeinfo" \
"(use \`make info' here to do that automatically)."
info:
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
@echo "Running Texinfo files through makeinfo..."
make -C $(BUILDDIR)/texinfo info
@echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo."
gettext:
$(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale
@echo
@echo "Build finished. The message catalogs are in $(BUILDDIR)/locale."
changes:
$(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes
@echo
@echo "The overview file is in $(BUILDDIR)/changes."
linkcheck:
$(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck
@echo
@echo "Link check complete; look for any errors in the above output " \
"or in $(BUILDDIR)/linkcheck/output.txt."
doctest:
$(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest
@echo "Testing of doctests in the sources finished, look at the " \
"results in $(BUILDDIR)/doctest/output.txt."

View File

@ -0,0 +1,54 @@
#FIG 3.2 Produced by xfig version 3.2.5b
Landscape
Center
Metric
A4
100.00
Single
-2
1200 2
1 1 0 1 0 7 50 -1 -1 0.000 1 0.0000 4500 1620 1260 585 4500 1620 5760 2205
2 3 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 9
1170 1350 1170 1890 2790 1890 2790 2070 3240 1620 2790 1170
2790 1350 1170 1350 1170 1350
2 3 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 9
5760 1350 5760 1890 7380 1890 7380 2070 7830 1620 7380 1170
7380 1350 5760 1350 5760 1350
2 1 1 1 0 7 50 -1 -1 1.000 0 0 -1 1 0 2
5 1 1.00 60.00 120.00
6210 540 6210 1440
2 1 1 1 0 7 50 -1 -1 1.000 0 0 -1 1 0 2
5 1 1.00 60.00 120.00
6210 2340 6210 1800
2 1 1 1 0 7 50 -1 -1 1.000 0 0 -1 1 0 2
5 1 1.00 60.00 120.00
1350 2520 1350 1800
2 1 1 1 0 7 50 -1 -1 1.000 0 0 -1 1 0 2
5 1 1.00 60.00 120.00
1350 360 1350 1440
3 0 1 1 0 7 50 -1 -1 1.000 0 0 1 5
5 1 1.00 60.00 120.00
2970 1665 3105 1125 3330 900 3600 765 3915 720
0.000 1.000 1.000 1.000 0.000
3 0 1 1 0 7 50 -1 -1 1.000 0 0 1 5
5 1 1.00 60.00 120.00
6030 1665 5895 1125 5670 900 5400 765 5040 720
0.000 1.000 1.000 1.000 0.000
4 2 0 50 -1 16 12 0.0000 4 195 750 1080 1665 producer\001
4 1 0 50 -1 16 12 0.0000 4 195 1785 4500 1575 HAProxy processing\001
4 1 0 50 -1 16 12 0.0000 4 195 1260 4500 1815 (including Lua)\001
4 0 0 50 -1 16 12 0.0000 4 105 855 7920 1665 consumer\001
4 0 0 50 -1 12 12 0.0000 4 150 600 1440 2205 set()\001
4 0 0 50 -1 12 12 0.0000 4 165 960 1440 2400 append()\001
4 0 0 50 -1 16 12 0.0000 4 150 1260 1260 2700 write functions\001
4 0 0 50 -1 16 12 0.0000 4 150 1230 1260 315 read functions\001
4 0 0 50 -1 12 12 0.0000 4 165 600 1440 540 dup()\001
4 0 0 50 -1 12 12 0.0000 4 165 600 1440 735 get()\001
4 0 0 50 -1 12 12 0.0000 4 165 1200 1440 930 get_line()\001
4 0 0 50 -1 12 12 0.0000 4 165 1440 1440 1125 get_in_len()\001
4 1 0 50 -1 12 12 0.0000 4 150 1080 4500 765 forward()\001
4 0 0 50 -1 16 12 0.0000 4 150 1260 6120 495 write functions\001
4 0 0 50 -1 12 12 0.0000 4 150 720 6300 1110 send()\001
4 0 0 50 -1 12 12 0.0000 4 165 1560 6255 2205 get_out_len()\001
4 0 0 50 -1 16 12 0.0000 4 150 1230 6120 2520 read functions\001
4 1 0 50 -1 16 12 0.0000 4 150 1650 4500 540 both side functions\001

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

242
doc/lua-api/conf.py Normal file
View File

@ -0,0 +1,242 @@
# -*- coding: utf-8 -*-
#
# haproxy-lua documentation build configuration file, created by
# sphinx-quickstart on Tue Mar 10 11:15:09 2015.
#
# This file is execfile()d with the current directory set to its containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.
import sys, os
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.insert(0, os.path.abspath('.'))
# -- General configuration -----------------------------------------------------
# If your documentation needs a minimal Sphinx version, state it here.
#needs_sphinx = '1.0'
# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = []
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# The suffix of source filenames.
source_suffix = '.rst'
# The encoding of source files.
#source_encoding = 'utf-8-sig'
# The master toctree document.
master_doc = 'index'
# General information about the project.
project = u'haproxy-lua'
copyright = u'2015, Thierry FOURNIER'
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = '1.0'
# The full version, including alpha/beta/rc tags.
release = '1.0'
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#language = None
# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = ['_build']
# The reST default role (used for this markup: `text`) to use for all documents.
#default_role = None
# If true, '()' will be appended to :func: etc. cross-reference text.
#add_function_parentheses = True
# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
#add_module_names = True
# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
#show_authors = False
# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'
# A list of ignored prefixes for module index sorting.
#modindex_common_prefix = []
# -- Options for HTML output ---------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
html_theme = 'default'
# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
#html_theme_options = {}
# Add any paths that contain custom themes here, relative to this directory.
#html_theme_path = []
# The name for this set of Sphinx documents. If None, it defaults to
# "<project> v<release> documentation".
#html_title = None
# A shorter title for the navigation bar. Default is the same as html_title.
#html_short_title = None
# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
#html_logo = None
# The name of an image file (within the static path) to use as favicon of the
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
#html_favicon = None
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
#html_last_updated_fmt = '%b %d, %Y'
# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
#html_use_smartypants = True
# Custom sidebar templates, maps document names to template names.
#html_sidebars = {}
# Additional templates that should be rendered to pages, maps page names to
# template names.
#html_additional_pages = {}
# If false, no module index is generated.
#html_domain_indices = True
# If false, no index is generated.
#html_use_index = True
# If true, the index is split into individual pages for each letter.
#html_split_index = False
# If true, links to the reST sources are added to the pages.
#html_show_sourcelink = True
# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
#html_show_sphinx = True
# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
#html_show_copyright = True
# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it. The value of this option must be the
# base URL from which the finished HTML is served.
#html_use_opensearch = ''
# This is the file name suffix for HTML files (e.g. ".xhtml").
#html_file_suffix = None
# Output file base name for HTML help builder.
htmlhelp_basename = 'haproxy-luadoc'
# -- Options for LaTeX output --------------------------------------------------
latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#'papersize': 'letterpaper',
# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',
# Additional stuff for the LaTeX preamble.
#'preamble': '',
}
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass [howto/manual]).
latex_documents = [
('index', 'haproxy-lua.tex', u'haproxy-lua Documentation',
u'Thierry FOURNIER', 'manual'),
]
# The name of an image file (relative to this directory) to place at the top of
# the title page.
#latex_logo = None
# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
#latex_use_parts = False
# If true, show page references after internal links.
#latex_show_pagerefs = False
# If true, show URL addresses after external links.
#latex_show_urls = False
# Documents to append as an appendix to all manuals.
#latex_appendices = []
# If false, no module index is generated.
#latex_domain_indices = True
# -- Options for manual page output --------------------------------------------
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
('index', 'haproxy-lua', u'haproxy-lua Documentation',
[u'Thierry FOURNIER'], 1)
]
# If true, show URL addresses after external links.
#man_show_urls = False
# -- Options for Texinfo output ------------------------------------------------
# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
('index', 'haproxy-lua', u'haproxy-lua Documentation',
u'Thierry FOURNIER', 'haproxy-lua', 'One line description of project.',
'Miscellaneous'),
]
# Documents to append as an appendix to all manuals.
#texinfo_appendices = []
# If false, no module index is generated.
#texinfo_domain_indices = True
# How to display URL addresses: 'footnote', 'no', or 'inline'.
#texinfo_show_urls = 'footnote'

759
doc/lua-api/index.rst Normal file
View File

@ -0,0 +1,759 @@
.. toctree::
:maxdepth: 2
How Lua runs in HAProxy
=======================
HAProxy Lua running contexts
----------------------------
The Lua code executed in HAProxy can be processed in 2 main modes. The first one
is the **initialisation mode**, and the second is the **runtime mode**.
* In the **initialisation mode**, we can perform DNS solves, but we cannot
perform socket I/O. In this initialisation mode, HAProxy still blocked during
the execution of the Lua program.
* In the **runtime mode**, we cannot perform DNS solves, but we can use sockets.
The execution of the Lua code is multiplexed with the requests processing, so
the Lua code seems to be run in blocking, but it is not the case.
The Lua code is loaded in one or more files. These files contains main code and
functions. Lua have 6 execution context.
1. The Lua file **body context**. It is executed during the load of the Lua file
in the HAProxy `[global]` section with the directive `lua-load`. It is
executed in initialisation mode. This section is use for configuring Lua
bindings in HAProxy.
2. The Lua **init context**. It is an Lua function executed just after the
HAProxy configuration parsing. The execution is in initialisation mode. In
this context the HAProxy environment are already initialized. It is useful to
check configuration, or initializing socket connections or tasks. These
functions are declared in the body context with the Lua function
`core.register_init()`. The prototype of the function is a simple function
without return value and without parameters, like this: `function fcn()`.
3. The Lua **task context**. It is an Lua function executed after the start
of the HAProxy scheduler, and just after the declaration of the task with the
Lua function `core.register_task()`. This context can be concurrent with the
traffic processing. It is executed in runtime mode. The prototype of the
function is a simple function without return value and without parameters,
like this: `function fcn()`.
4. The **action context**. It is an Lua function conditionally executed. These
actions are declared by the HAProxy directives "`tcp-request content lua
<function>`", "`tcp-response content lua <function>`", "`http-request lua
<function>`" and "`http-response lua <function>`". The prototype of the
Lua called function is a function with doesn't returns anything and that take
an object of class TXN as entry. `function fcn(txn)`
5. The **sample-fetch context**. This function takes a TXN object as entry
argument and returns a string. These types of function cannot execute any
blocking function. They are useful to aggregate some of original HAProxy
sample-fetches and return the result. The prototype of the function is
`function string fcn(txn)`. These functions can be registered with the Lua
function `core.register_fetches()`. Each declared sample-fetch is prefixed by
the string "lua.".
**NOTE**: It is possible that this function cannot found the required data
in the original HAProxy sample-fetches, in this case, it cannot return the
result. This case is not yet supported
6. The **converter context**. It is an Lua function that takes a string as input
and returns another string as output. These types of function are stateless,
it cannot access to any context. They don't execute any blocking function.
The call prototype is `function string fcn(string)`. This function can be
registered with the Lua function `core.register_converters()`. Each declared
converter is prefixed by the string "lua.".
HAProxy Lua Hello world
-----------------------
HAProxy configuration file (`hello_world.conf`):
::
global
lua-load hello_world.lua
listen proxy
bind 127.0.0.1:10001
tcp-request content lua hello_world
HAProxy Lua file (`hello_world.lua`):
.. code-block:: lua
function hello_world(txn)
local res = txn:res_channel()
res:send("hello world\n")
end
How to start HAProxy for testing this configuration:
::
./haproxy -f hello_world.conf
On other terminal, you can test with telnet:
::
#:~ telnet 127.0.0.1 10001
hello world
Core class
==========
.. js:class:: core
The "core" class contains all the HAProxy core functions. These function are
useful for the controlling the execution flow, registering hooks, manipulating
global maps or ACL, ...
"core" class is basically provided with HAProxy. No `require` line is
required to uses these function.
The "core" class is static, t is not possible to create a new object of this
type.
.. js:function:: core.add_acl(filename, key)
**context**: init, task, action, sample-fetch, converter
Add the ACL *key* in the ACLs list referenced by the file *filename*.
:param string filename: the filename that reference the ACL entries.
:param string key: the key which will be added.
.. js:function:: core.del_acl(filename, key)
**context**: init, task, action, sample-fetch, converter
Delete the ACL entry referenced by the key *key* in the list of ACLs
referenced by *filename*.
:param string filename: the filename that reference the ACL entries.
:param string key: the key which will be deleted.
.. js:function:: core.del_map(filename, key)
**context**: init, task, action, sample-fetch, converter
Delete the map entry indexed with the specified key in the list of maps
referenced by his filename.
:param string filename: the filename that reference the map entries.
:param string key: the key which will be deleted.
.. js:function:: core.msleep(milliseconds)
**context**: body, init, task, action
The `core.msleep()` stops the Lua execution between specified milliseconds.
:param integer milliseconds: the required milliseconds.
.. js:function:: core.register_converters(name, func)
**context**: body
Register an Lua function executed as converter. All the registered converters
can be used in HAProxy with the prefix "lua.". An converter get a string as
input and return a string as output. The registered function can take up to 9
values as parameter. All the value are strings.
:param string name: is the name of the converter.
:param function func: is the Lua function called to work as converter.
The prototype of the Lua function used as argument is:
.. code-block:: lua
function(str, [p1 [, p2 [, ... [, p5]]]])
..
* **str** (*string*): this is the input value automatically converted in
string.
* **p1** .. **p5** (*string*): this is a list of string arguments declared in
the haroxy configuration file. The number of arguments doesn't exceed 5.
The order and the nature of these is conventionally choose by the
developper.
.. js:function:: core.register_fetches(name, func)
**context**: body
Register an Lua function executed as sample fetch. All the registered sample
fetchs can be used in HAProxy with the prefix "lua.". A Lua sample fetch
return a string as output. The registered function can take up to 9 values as
parameter. All the value are strings.
:param string name: is the name of the converter.
:param function func: is the Lua function called to work as sample fetch.
The prototype of the Lua function used as argument is:
.. code-block:: lua
string function(txn, [p1 [, p2 [, ... [, p5]]]])
..
* **txn** (*class txn*): this is the txn object associated with the current
request.
* **p1** .. **p5** (*string*): this is a list of string arguments declared in
the haroxy configuration file. The number of arguments doesn't exceed 5.
The order and the nature of these is conventionally choose by the
developper.
* **Returns**: A string containing some data, ot nil if the value cannot be
returned now.
lua example code:
.. code-block:: lua
core.register_fetches("hello", function(txn)
return "hello"
end)
..
HAProxy example configuration:
::
frontend example
http-request redirect location /%[lua.hello]
.. js:function:: core.register_init(func)
**context**: body
Register a function executed after the configuration parsing. This is useful
to check any parameters.
:param fuction func: is the Lua function called to work as initializer.
The prototype of the Lua function used as argument is:
.. code-block:: lua
function()
..
It takes no input, and no output is expected.
.. js:function:: core.register_task(func)
**context**: body, init, task, action, sample-fetch, converter
Register and start independent task. The task is started when the HAProxy
main scheduler starts. For example this type of tasks can be executed to
perform complex health checks.
:param fuction func: is the Lua function called to work as initializer.
The prototype of the Lua function used as argument is:
.. code-block:: lua
function()
..
It takes no input, and no output is expected.
.. js:function:: core.set_nice(nice)
**context**: task, action, sample-fetch, converter
Change the nice of the current task or current session.
:param integer nice: the nice value, it must be between -1024 and 1024.
.. js:function:: core.set_map(filename, key, value)
**context**: init, task, action, sample-fetch, converter
set the value *value* associated to the key *key* in the map referenced by
*filename*.
.. js:function:: core.sleep(int seconds)
**context**: body, init, task, action
The `core.sleep()` functions stop the Lua execution between specified seconds.
:param integer seconds: the required seconds.
.. js:function:: core.tcp()
**context**: init, task, action
This function returns a new object of a *socket* class.
:returns: A socket class object.
.. js:function:: socket core.yield()
**context**: task, action, sample-fetch, converter
Give back the hand at the HAProxy scheduler. It is used when the LUA
processing consumes a lot of processing time.
Fetches class
=============
.. js:class:: Fetches
This class contains a lot of internal HAProxy sample fetches. See the
HAProxy documentation for more information about her usage.
Converters class
================
.. js:class:: Converters
This class contains a lot of internal HAProxy sample converters. See the
HAProxy documentation for more information about her usage.
Channel class
=============
.. js:class:: Channel
HAProxy uses two buffers for the processing of the requests. The first one is
used with the request data (from the client to the server) and the second is
used for the response data (from the server to the client).
Each buffer contains two types of data. The first type is the incoming data
waiting for a processing. The second part is the outgoing data already
processed. Usually, the incoming data is processed, after it is tagged as
outgoing data, and finally it is sent. The following functions provides tools
for manipulating these data in a buffer.
The following diagram shows where the channel class function are applied.
**Warning**: It is not possible to read from the response in request action,
and it is not possible to read for the request channel in response action.
.. image:: _static/channel.png
.. js:function:: channel.dup(channel)
This function returns a string that contain the entire buffer. The data is
not remove from the buffer and can be reprocessed later.
If the buffer cant receive more data, a 'nil' value is returned.
:param class_channel channel: The manipulated channel.
:returns: a string containig all the avalaible data or nil.
.. js:function:: channel.get(channel)
This function returns a string that contain the entire buffer. The data is
consumed from the buffer.
If the buffer cant receive more data, a 'nil' value is returned.
:param class_channel channel: The manipulated channel.
:returns: a string containig all the avalaible data or nil.
.. js:function:: channel.get_line(channel)
This function returns a string that contain the first line of the buffer. The
data is consumed. If the data returned doesn't contains a final '\n' its
assumed than its the last available data in the buffer.
If the buffer cant receive more data, a 'nil' value is returned.
:param class_channel channel: The manipulated channel.
:returns: a string containig the avalaiable line or nil.
.. js:function:: channel.set(channel, string)
This function replace the content of the buffer by the string. The function
returns the copied length, otherwise, it returns -1.
The data set with this function are not send. They wait for the end of
HAProxy processing, so the buffer can be full.
:param class_channel channel: The manipulated channel.
:param string string: The data which will sent.
:returns: an integer containing the amount of butes copyed or -1.
.. js:function:: channel.append(channel, string)
This function append the string argument to the content of the buffer. The
function returns the copied length, otherwise, it returns -1.
The data set with this function are not send. They wait for the end of
HAProxy processing, so the buffer can be full.
:param class_channel channel: The manipulated channel.
:param string string: The data which will sent.
:returns: an integer containing the amount of butes copyed or -1.
.. js:function:: int channel.send(channel, string)
This function required immediate send of the data. Unless if the connection
is close, the buffer is regularly flushed and all the string can be sent.
:param class_channel channel: The manipulated channel.
:param string string: The data which will sent.
:returns: an integer containing the amount of butes copyed or -1.
.. js:function:: int channel.get_in_length(channel)
This function returns the length of the input part of the buffer.
:param class_channel channel: The manipulated channel.
:returns: an integer containing the amount of avalaible bytes.
.. js:function:: int channel.get_out_length(channel)
This function returns the length of the output part of the buffer.
:param class_channel channel: The manipulated channel.
:returns: an integer containing the amount of avalaible bytes.
.. js:function:: channel.forward(channel, int)
This function transfer bytes from the input part of the buffer to the output
part.
:param class_channel channel: The manipulated channel.
:param integer int: The amount of data which will be forwarded.
TXN class
=========
.. js:class:: TXN
The txn class contain all the functions relative to the http or tcp
transaction (Note than a tcp stream is the same than a tcp transaction, but
an HTTP transaction is not the same than a tcp stream).
The usage of this class permits to retrieve data from the requests, alter it
and forward it.
All the functions provided by this class are available in the context
**sample-fetches** and **actions**.
.. js:attribute:: txn.c
This attribute contains a Converters class object.
.. js:attribute:: txn.sc
This attribute contains a Converters class object. The functions of
this object returns always a string.
.. js:attribute:: txn.f
This attribute contains a Fetches class object.
.. js:attribute:: txn.sf
This attribute contains a Fetches class object. The functions of
this object returns always a string.
.. js:attribute:: txn.req
This attribute contains a channel class object for the request buffer.
.. js:attribute:: txn.res
This attribute contains a channel class object for the response buffer.
.. js:function:: txn.get_priv(txn)
Return Lua data stored in the current transaction (with the `txn.set_priv()`)
function. If no data are stored, it returns a nil value.
:param class_txn txn: The class txn object containing the data.
:returns: the opaque data previsously stored, or nil if nothing is
avalaible.
.. js:function:: txn.set_priv(txn, data)
Store any data in the current HAProxy transaction. This action replace the
old stored data.
:param class_txn txn: The class txn object containing the data.
:param opaque data: The data which is stored in the transaction.
.. js:function:: txn.get_headers(txn)
This function returns an array of headers.
:param class_txn txn: The class txn object containing the data.
:returns: an array of headers.
.. js:function:: txn.close(txn)
This function close the transaction and the associated session. It can be
used when a critical error is detected.
:param class_txn txn: The class txn object containing the data.
.. js:function:: txn.http.redirect(txn, location)
Not yet avalaible.
.. js:function:: txn.http.req.add_header(txn, name, value)
Not yet avalaible.
.. js:function:: txn.http.req.set_method(txn, string)
Not yet avalaible.
.. js:function:: txn.http.req.set_path(txn, string)
Not yet avalaible.
.. js:function:: txn.http.req.set_query(txn, string)
Not yet avalaible.
.. js:function:: txn.http.req.set_uri(txn, string)
Not yet avalaible.
.. js:function:: txn.http.req.set_header(txn, name, value)
Not yet avalaible.
.. js:function:: txn.http.req.del_header(txn, name)
Not yet avalaible.
.. js:function:: txn.http.req.replace_header(txn, name, regex, string)
Not yet avalaible.
.. js:function:: txn.http.req.replace_value(txn, name, regex, string)
Not yet avalaible.
.. js:function:: txn.http.res.set_header(txn, name, value)
Not yet avalaible.
.. js:function:: txn.http.res.del_header(txn, name)
Not yet avalaible.
.. js:function:: txn.http.res.replace_header(txn, name, regex, string)
Not yet avalaible.
.. js:function:: txn.http.res.replace_value(txn, name, regex, string)
Not yet avalaible.
Socket class
============
.. js:class:: Socket
This class must be compatible with the Lua Socket class. Only the 'client'
functions are available. See the Lua Socket documentation:
`http://w3.impa.br/~diego/software/luasocket/tcp.html
<http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
.. js:function:: socket.close(socket)
Closes a TCP object. The internal socket used by the object is closed and the
local address to which the object was bound is made available to other
applications. No further operations (except for further calls to the close
method) are allowed on a closed socket.
:param class_socket socket: Is the manipulated socket.
Note: It is important to close all used sockets once they are not needed,
since, in many systems, each socket uses a file descriptor, which are limited
system resources. Garbage-collected objects are automatically closed before
destruction, though.
.. js:function:: socket.connect(socket, address, port)
Attempts to connect a socket object to a remote host.
Address can be an IP address or a host name. Port must be an integer number
in the range [1..64K).
In case of error, the method returns nil followed by a string describing the
error. In case of success, the method returns 1.
:param class_socket socket: Is the manipulated socket.
:returns: 1 or nil.
Note: The function socket.connect is available and is a shortcut for the
creation of client sockets.
Note: Starting with LuaSocket 2.0, the settimeout method affects the behavior
of connect, causing it to return with an error in case of a timeout. If that
happens, you can still call socket.select with the socket in the sendt table.
The socket will be writable when the connection is established.
.. js:function:: socket.connect_ssl(socket, address, port)
Same behavior than the function socket:connect, but uses SSL.
:param class_socket socket: Is the manipulated socket.
:returns: 1 or nil.
.. js:function:: socket.getpeername(socket)
Returns information about the remote side of a connected client object.
Returns a string with the IP address of the peer, followed by the port number
that peer is using for the connection. In case of error, the method returns
nil.
:param class_socket socket: Is the manipulated socket.
:returns: a string containing the server information.
.. js:function:: socket.getsockname(socket)
Returns the local address information associated to the object.
The method returns a string with local IP address and a number with the port.
In case of error, the method returns nil.
:param class_socket socket: Is the manipulated socket.
:returns: a string containing the client information.
.. js:function:: socket.receive(socket, [pattern [, prefix]])
Reads data from a client object, according to the specified read pattern.
Patterns follow the Lua file I/O format, and the difference in performance
between all patterns is negligible.
:param class_socket socket: Is the manipulated socket.
:param string|integer pattern: Describe what is required (see below).
:param string prefix: A string which will be prefix the returned data.
:returns: a string containing the required data or nil.
Pattern can be any of the following:
* **`*a`**: reads from the socket until the connection is closed. No
end-of-line translation is performed;
* **`*l`**: reads a line of text from the socket. The line is terminated by a
LF character (ASCII 10), optionally preceded by a CR character
(ASCII 13). The CR and LF characters are not included in the
returned line. In fact, all CR characters are ignored by the
pattern. This is the default pattern.
* **number**: causes the method to read a specified number of bytes from the
socket. Prefix is an optional string to be concatenated to the
beginning of any received data before return.
If successful, the method returns the received pattern. In case of error, the
method returns nil followed by an error message which can be the string
'closed' in case the connection was closed before the transmission was
completed or the string 'timeout' in case there was a timeout during the
operation. Also, after the error message, the function returns the partial
result of the transmission.
Important note: This function was changed severely. It used to support
multiple patterns (but I have never seen this feature used) and now it
doesn't anymore. Partial results used to be returned in the same way as
successful results. This last feature violated the idea that all functions
should return nil on error. Thus it was changed too.
.. js:function:: socket.send(socket, data [, start [, end ]])
Sends data through client object.
:param class_socket socket: Is the manipulated socket.
:param string data: The data that will be sent.
:param integer start: The start position in the buffer of the data which will
be sent.
:param integer end: The end position in the buffer of the data which will
be sent.
:returns: see below.
Data is the string to be sent. The optional arguments i and j work exactly
like the standard string.sub Lua function to allow the selection of a
substring to be sent.
If successful, the method returns the index of the last byte within [start,
end] that has been sent. Notice that, if start is 1 or absent, this is
effectively the total number of bytes sent. In case of error, the method
returns nil, followed by an error message, followed by the index of the last
byte within [start, end] that has been sent. You might want to try again from
the byte following that. The error message can be 'closed' in case the
connection was closed before the transmission was completed or the string
'timeout' in case there was a timeout during the operation.
Note: Output is not buffered. For small strings, it is always better to
concatenate them in Lua (with the '..' operator) and send the result in one
call instead of calling the method several times.
.. js:function:: socket.setoption(socket, option [, value])
Just implemented for compatibility, this cal does nothing.
.. js:function:: socket.settimeout(socket, value [, mode])
Changes the timeout values for the object. All I/O operations are blocking.
That is, any call to the methods send, receive, and accept will block
indefinitely, until the operation completes. The settimeout method defines a
limit on the amount of time the I/O methods can block. When a timeout time
has elapsed, the affected methods give up and fail with an error code.
The amount of time to wait is specified as the value parameter, in seconds.
The timeout modes are bot implemented, the only settable timeout is the
inactivity time waiting for complete the internal buffer send or waiting for
receive data.
:param class_socket socket: Is the manipulated socket.
:param integer value: The timeout value.
External Lua libraries
======================
A lot of useful lua libraries can be found here:
* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
Redis acces:
* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
This is an example about the usage of the Redis library with HAProxy. Note that
each call of any function of this library can throw an error if the socket
connection fails.
.. code-block:: lua
-- load the redis library
local redis = require("redis");
function do_something(txn)
-- create and connect new tcp socket
local tcp = core.tcp();
tcp:settimeout(1);
tcp:connect("127.0.0.1", 6379);
-- use the redis library with this new socket
local client = redis.connect({socket=tcp});
client:ping();
end
OpenSSL:
* `http://mkottman.github.io/luacrypto/index.html
<http://mkottman.github.io/luacrypto/index.html>`_
* `https://github.com/brunoos/luasec/wiki
<https://github.com/brunoos/luasec/wiki>`_