I am trying to write a lua fetch function to be able to redirect POST requests according to a value in the POST data.
The problem I am incurring is that I cannot find a way to make HAProxy wait for all the POST data to arrive - if for some reason this arrives in two chunks, haproxy will only make its decision using just the first chunk.
Here are my haproxy.cfg and mylua.lua. The current configuration tries to match the string “TWO” in the post data and if so the request should get passed to backend be2
haproxy.cfg:
`global
lua-load ./mylua.lua
defaults
mode http
timeout connect 3s
timeout client 8s
timeout server 3s
frontend fe
bind 127.0.0.1:8001
mode http
acl acl_be2 lua.check_post -m str “TWO”
use_backend be2 if acl_be2
default_backend be1
backend be1
mode http
server s 127.0.0.1:9003
backend be2
mode http
server s 127.0.0.1:2222
`
mylua.lua:
`function check_post(txn)
local all_data=txn.req:dup()
if string.match(all_data, “TWO”) then
return "TWO"
else
return "ONE"
end
end
core.register_fetches(“check_post”, check_post)
`
The problem occurs only when the request gets chunked. For example the request below gets delivered to backend be1 instead of be2:
sh-4.2# (echo 'POST / HTTP/1.1 User-Agent: curl/7.26.0 Host: 127.0.0.1:8001 Accept: */* Content-Length: 11 Content-Type: application/x-www-form-urlencoded '; sleep 2; echo '1234TWO1234'; ) | nc 127.1 8001
However if I remove the “sleep 2”, the request is not chunked and request goes to backend be2 as expected.
My question is how can I perform such decisions by matching content on the POST data and be sure that this will always work?