You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a pure Lua HTTP 1.1 library for HAProxy. It should work on any modern
HAProxy version (1.7+)
The library is loosely modeled after Python's Requests Library using the same
attribute names and similar calling conventions for "HTTP verb" methods (where
we use Lua specific named parameter support)
In addition to client side, the library also supports server side request
parsing, where we utilize HAProxy Lua API for all heavy lifting (i.e. HAProxy
handles client side connections, parses the headers and gives us access to
request body).
Usage
After downloading this library, you will need to move it into your Lua package
package path, to be able to use it from your script. In later HAProxy versions,
there is a lua-prepend-path directive which can make your life easier.
Basic usage for parsing client requests, constructing responses, or sending
custom requests to external servers is demonstrated bellow. You can use this in
your own Lua actions or services:
localhttp=require('http')
localfunctionmain(applet)
-- 1) Parse client side request and print received headerslocalreq=http.request.parse(applet)
fork, vinreq:get_headers() docore.Debug(k..": " ..v)
end-- You can also parse submitted form datalocalform, err=req:parse_multipart()
-- 2) Send request to external server (please note there is no DNS-- support for Lua on HAProxylocalres, err=http.get{url="https://1.2.3.4",
headers={host="example.net", ["x-test"]={"a", "b"}}
}
ifresthenfork, vinres:get_headers() docore.Debug(k..": " ..v)
end-- We can access the response body in content attributecore.Debug(res.content)
elsecore.Debug(err)
end-- 3) Send response to client sidehttp.response.create{status_code=200, content="Hello World"}:send(applet)
endcore.register_service("test", "http", main)
Naturally, you need to add your script to main haproxy configuration:
global
...
lua-load test.lua
frontend test
...
http-request use-service lua.test
About
Simple Lua HTTP helper && client for use with HAProxy.