Welcome! Log In Create A New Profile

Advanced

Reverse String (or get 2nd level domain sample)?

Posted by Daniel Schneller 
Daniel Schneller
Reverse String (or get 2nd level domain sample)?
June 25, 2018 12:40PM
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

__________________________________________
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.
Daniel Schneller
Re: Reverse String (or get 2nd level domain sample)?
June 25, 2018 03:20PM
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 <[email protected]> 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.
>
>
Hi,

converters are just simple C functions, (or could be Lua code as well), and
are quite trivial to write.
Instead of creating a converter that reverse the order of chars in a
string, I would rather patch current "word" converter to support negative
integers.
IE: -2 would means you extract the second word, starting at the end of the
string.

Baptiste



On Mon, Jun 25, 2018 at 12:29 PM, Daniel Schneller <
[email protected]> 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 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
>
> __________________________________________
> 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.
>
>
>
Jarno Huuskonen
Re: Reverse String (or get 2nd level domain sample)?
June 30, 2018 08:10AM
Hi,

On Fri, Jun 29, Baptiste wrote:
> converters are just simple C functions, (or could be Lua code as well), and
> are quite trivial to write.
> Instead of creating a converter that reverse the order of chars in a
> string, I would rather patch current "word" converter to support negative
> integers.
> IE: -2 would means you extract the second word, starting at the end of the
> string.

No need to patch "word" for negative indexes. The functionality is
already there:
commit 9631a28275b7c04f441f7d1c3706a765586844e7
Author: Marcin Deranek <[email protected]>
Date: Mon Apr 16 14:30:46 2018 +0200

MEDIUM: sample: Extend functionality for field/word converters

Extend functionality of field/word converters, so it's possible
to extract field(s)/word(s) counting from the beginning/end and/or
extract multiple fields/words (including separators) eg.

str(f1_f2_f3__f5),field(2,_,2) # f2_f3
str(f1_f2_f3__f5),field(2,_,0) # f2_f3__f5
str(f1_f2_f3__f5),field(-2,_,3) # f2_f3_
str(f1_f2_f3__f5),field(-3,_,0) # f1_f2_f3

str(w1_w2_w3___w4),word(3,_,2) # w3___w4
str(w1_w2_w3___w4),word(2,_,0) # w2_w3___w4
str(w1_w2_w3___w4),word(-2,_,3) # w1_w2_w3
str(w1_w2_w3___w4),word(-3,_,0) # w1_w2;

-Jarno

> On Mon, Jun 25, 2018 at 12:29 PM, Daniel Schneller <
> [email protected]> 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 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

--
Jarno Huuskonen
Sorry, only registered users may post in this forum.

Click here to login