Hi Joao,

On Sun, May 09, 2021 at 07:22:14PM -0300, Joao Morais wrote:
> Hello list, following a few questions about Lua and HAProxy vars:
> 
> Is there a way to read req scoped vars from a Lua script registered with
> core.register_service()? My attempts so far didn't succeed, I need to copy
> the value to a txn scoped var before call the service.

While I don't know exactly how these are accessed from Lua, the req.* vars
only exist during the request processing. As soon as the request is emitted,
they disappear. And services work exactly like a server in that they're
outside of the request processing block (they're called "applets" in haproxy's
internal vocabulary). So the request varables do not exist anymore when a
service is called.

> Another question, one of these vars has a comma separated list of strings
> that I declare with str(itemOne,itemTwo), but HAProxy complains with:
> 
>     error detected in proxy 'l' while parsing 'http-request 
> set-var(txn.myvar)' rule : fetch method 'str' : expected ')' before 
> ',itemTwo)'.

By default, the comma is an argument delimiter. This is why quotes are
needed. But quotes are processed at the top level of the config parser
since they can protect escape. Thus two levels of quotes are supported:
  - one at the top level
  - one at the argument level

You just need to embed them like this:

   'str("one,two")'

It's parsed like this:

   'str("First,Second","Third,Fourth")'
   :    |<---------->| |<---------->| :
   :      first arg      second arg   :
   |<-------------------------------->|
    single word for the config parser,
    protecting the quotes inside.

It's not very different from the way escaping works in shell when you
start to encapsulate commands, e.g. with sh -c "command 'arg1 arg2'".

Note that if you use double quotes outside, a '$' will resolve a
variable anywhere in the string. If you use single quotes outside,
your '$' chars will stay as-is.

Hoping this helps,
Willy

Reply via email to