Apache regex backreferences UNREACHABLE in httpd 2.4?
Asked Answered
C

3

8

I Love the new apache httpd 2.4 with lots of new cool stuff !

ap_expr is one of these new very promising features,

..BUT the following SSI snippet don't work as expected:

{{if expr="v('HTTP_SESSION') =~ /-user=([^&]+)/"}} 
{{set var="user" value="$1"}} 
{{endif}}

The if is working BUT the var isn't set ! This doesn't make any sense.

error.log says:

.. AH01330: regex capture $1 is out of range

The doc (http://httpd.apache.org/docs/2.4/expr.html#other) is confusing and have no samples anywhere near.

I know that there is a legacy (workaround) switch for SSI.. but I don't want to use it since old Start- and End-tags are forced Legacy

Doing similar regex-parsing-tricks w SetEnvIfExpr is not helping either

Crosswalk answered 18/4, 2013 at 19:48 Comment(1)
"They can normally only be used in the same expression as the matching regex, but some modules allow special uses." I imagine that's the issue, and you have to fit the assignment into the "same" expression, whatever that domain is.Profitsharing
G
1

by changing

{{if expr="v('HTTP_SESSION') =~ /-user=([^&]+)/"}} 
    {{set var="user" value="$1"}} 
{{endif}}

to

{{if expr="v('HTTP_SESSION') =~ /-user=([^&]+)/"}} 
    {{set var="user" value="$0"}}
    {{if expr="v('user') =~ /([^&]+)$/"}}
         {{set var="user" value="$0"}}
    {{endif}}
{{endif}}

one can work around the problem using the fact that $0 seems to work

Gink answered 20/4, 2015 at 7:57 Comment(1)
A life safer! (not really, but this helped me a lot)Terbium
S
0

According to the documentation:

Regular expression backreferences The strings $0 ... $9 allow to reference the capture groups from a previously executed, successfully matching regular expressions. They can normally only be used in the same expression as the matching regex, but some modules allow special uses.

You are using a capturing group and you are referencing that using index 1, this should be ok using a standard regex engine, but according to the documentation you can reference from $0...$9. I guess apache is filling the inde 0 with your capturing group instead of 1 and that's you get the regex capture $1 is out of range

Change the index $1 to $0 by using:

{{if expr="v('HTTP_SESSION') =~ /-user=([^&]+)/"}} 
{{set var="user" value="$0"}} 
{{endif}}
Stocktonontees answered 8/4, 2015 at 19:45 Comment(1)
Seems to be the case, but I don't think Apache devs would reinvent the wheel!Maury
P
0
{{if expr="v('HTTP_SESSION') =~ /-user=([^&]+)/ && $1 =~ /(.+)/"}} 
    {{set var="user" value="$0"}} 
{{endif}}
Publicspirited answered 30/9, 2015 at 13:2 Comment(1)
This is just a lump of code that is almost identical to the code in other answers. For this code to be useful it should have an explanation of what it does and why it answers the question. Given that it is so similar to the code in other answers it should also explain why it is different to them.Longwinded

© 2022 - 2024 — McMap. All rights reserved.