Hi again!

I found a working config using the map_regm converter.
I think it is somewhat overcomplicated for what it is supposed to achieve, but 
for now it works.

Leaving this here for reference:

    # Remove port numbers from the Host header -- we do not rely on different 
ports for the same domain, and this makes ACL matching clearer
    http-request replace-value Host '(.*):.*' '\1'

    # Store the (now port-free) request Host in a transaction scoped variable 
for use in response ACLs
    http-request set-var(txn.host) req.hdr(Host)

    # Store the 2nd level domain (lower case) as the distributor. This uses a 
simple map file with just a single
    # regex, because the inline regsub function does not support backrefs which 
are needed for variable number of subdomains.
    http-request set-var(txn.distributor) 
var(txn.host),map_regm(distributors.map,"unknown"),lower

    # Add a X-Distributor header for the application, overwriting anything the 
client may have claimed
    http-request set-header X-Distributor %[var(txn.distributor)]

The distributors.map file contents looks like this:

    (.*\.)+(.*)\.(.*) \2

Looks more complicated than it is. The first "(.*\.)+" greedily matches 
subdomains (in our case only domains with at least three parts are valid) and 
their trailing dots.
The second capture group matches the second level domain, followed by a dot, 
and then the final capture group "(.*)" for the top level domain.
The final one doesn't _have_ to be a group, because I drop the top level domain 
anyway. , but I find it more readable this way.

Anything that matches this regex is replaced with just the value of the 2nd 
capture group (i. e. the 2nd level domain).

(tested with haproxy 1.8, but this should also work with earlier versions IMO).

Cheers,
Daniel





> On 25. Jun 2018, at 12:29, Daniel Schneller 
> <daniel.schnel...@centerdevice.com> wrote:
> 
> Hi!
> 
> Just double checking to make sure I am not simply blind: Is there a way to 
> reverse a string using a sample converter?
> 
> Background: I need to extract just the second level domain from the host 
> header. So for sub.sample.example.com <http://sub.sample.example.com/> I need 
> to fetch "example".
> 
> Using the "word" converter and a "." as the separator I can get at the 
> individual components, but because the number of nested subdomains varies, I 
> cannot use that directly.
> 
> My idea was to just reverse the full domain (removing a potential port number 
> first), get word(2) and reverse again. Is that possible? Or is there an even 
> better function I can use? I am thinking this must be a common use case, but 
> googling "haproxy" and "reverse" will naturally turn up lots of results 
> talking about "reverse proxying".
> 
> If possible, I would like to avoid using maps to keep this thing as generic 
> as possible.
> 
> Thanks a lot!
> 
> Daniel
> 
> 
> --
> Daniel Schneller
> Principal Cloud Engineer
> 
> CenterDevice GmbH
> Rheinwerkallee 3
> 53227 Bonn
> www.centerdevice.com <http://www.centerdevice.com/>
> 
> __________________________________________
> Geschäftsführung: Dr. Patrick Peschlow, Dr. Lukas Pustina, Michael Rosbach, 
> Handelsregister-Nr.: HRB 18655, HR-Gericht: Bonn, USt-IdNr.: DE-815299431
> 
> Diese E-Mail einschließlich evtl. beigefügter Dateien enthält vertrauliche 
> und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige 
> Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie 
> bitte sofort den Absender und löschen Sie diese E-Mail und evtl. beigefügter 
> Dateien umgehend. Das unerlaubte Kopieren, Nutzen oder Öffnen evtl. 
> beigefügter Dateien sowie die unbefugte Weitergabe dieser E-Mail ist nicht 
> gestattet.
> 
> 

Attachment: signature.asc
Description: Message signed with OpenPGP

Reply via email to