HAProxy community

Is there any function to get the length of a string? Proxypass example inside also :)


First of all, thank you all for reading this post.

I’m looking for a simple method to just get the length of a string, and I supposed that there should be a converter to do this but I can’t find anything. This surprised me because there is a lot of advanced features that can be acomplished with the converters and this is a simple feature, so maybe I’m missing something?

Just to give you context and maybe this could help someone, I was looking for a way to make easier our current deployment of a proxypass with HAProxy, so my best aproach right now is this:

frontend ft-http-proxypass
	bind :80
	http-request set-var(req.proxypass_be) path,field(2,'/'),map(/etc/haproxy/backends.map) # Stores into a variable the backend that is going to use
	acl is_proxypass var(req.proxypass_be) -m found # Just checks that the requested path is included in the mapfile
	reqrep ^([^\ :]*\ \/)[^\/\ ]+\/?(.*)   \1\2     if is_proxypass    # Replaces the first path for a '/'. For example: 
			# /path1 			-> /
			# /path1/ 			-> / 
			# /path1/subpath 	-> /subpath

	use_backend %[var(req.proxypass_be)] if is_proxypass
backend bk-http-site1
	server server1

backend bk-http-site2
	server server1
backend bk-http-site3
	server server1

Just to complete the example, this could be the content of the map file:
path1 bk-http-site1
path2 bk-http-site2
path3 bk-http-site3

I’m almost happy with it, but I would like to replace the reqrep line with a ‘http-request set-path’ in conjunction with the converter ‘bytes’ (could be used like a substring function) and using the lenght of the first subpath as an offset of this bytes converter.

Is it any way to do this with the tools provided by HAProxy or is it needed to use an external tool like a lua script?

I’m using haproxy 1.8.8 btw.

Thank you!

To get the length of a string, use -m len

Hello. Thank you for your help and sorry for my late reply, this has been a crazy week.

I knew that parameter to compare something in a ACL. But can it be used to simply get the lenght?

I tried to store the length of a string into a variable with your configuration, with no luck. How should I use it? Those were my tries and the check of the configuration always give me an error:

http-request set-var(txn.proxypass_len) path, -m len
http-request set-var(txn.proxypass_len) path -m len
http-request set-var(txn.proxypass_len) %[path -m len]

Thank you!