logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Apache View previous topic :: View next topic
Reply to topic   Topic: Appending or joining ENV with SetEnv or SetEnvIf
Author
csdude55



Joined: 22 Jan 2023
Posts: 17
Location: USA, Wilkesboro

PostPosted: Sun 29 Jan '23 22:51    Post subject: Appending or joining ENV with SetEnv or SetEnvIf Reply with quote

I'm getting myself caught in another bottleneck.

Can you suggest any method, trick, or hack that would either:

(a) append an existing environment variable that was set using SetEnvIf; or

(b) join two or more environment variables with a string delimiter in the expression section of SetEnvIfExpr


I have %{ENV:foo}, and if a condition matches then I need to append to it; eg,

Code:
# line breaks added for readability
SetEnvIfExpr "%{ENV:foo} =~ /(bar)/"
  %{ENV:lorem}=%{ENV:lorem}$1


But, of course, that doesn't work, because apparently I can't refer to ENV variables in the value? It seems that everything in the value is taken literally except for backreferences.


The second idea was to use two conditions and refer back to both, like so:

Code:
SetEnvIfExpr "%{ENV:foo} =~ /(bar)/ && %{ENV:lorem} =~ /(.+)/"
  %{ENV:lorem}=$2$1


But it looks like the second match just overwrites $1 instead of becoming $2, so that doesn't work.


My third idea was to join "foo" and "lorem" with a delimiter, then I could do something like this:

Code:
SetEnvIfExpr "(%{ENV:foo} . '|:|' . %{ENV:lorem}) =~ /(bar)\|:\|(.*)/"
  %{ENV:lorem}=$2$1


But that throws an error, and I can't find any way to join the two ENVs.


I'm open to any and all suggestions! If I can't get past this, I'm gonna have to undo about 3 weeks worth of work :-/
Back to top
tangent
Moderator


Joined: 16 Aug 2020
Posts: 312
Location: UK

PostPosted: Mon 30 Jan '23 23:13    Post subject: Reply with quote

You're nearly there with your final SetEnvIfExpr construct. Try this variant, to pick up the two backreferences (I've used # as the delimiter).

Code:
SetEnvIfExpr "env('foo') . '#' . env('lorem') =~ /.*(bar).*#(.+)/" lorem=$2$1

Assuming initial variable values lorem=ipsum, and foo=bar, this should give lorem=ipsumbar. However, I believe a word of caution is needed over editing existing variables.

Depending on where these statements are scoped in your configuration (global, vhost), and whether the request URI (e.g. /example) causes a sub-request (e.g. /example/index.html), you may find lorem ends up as ipsumbarbar since the SetEnvIfExpr statement will be executed more than once.

My workaround for this would be to manipulate the variables with mod_rewrite, where you can prevent the rules being executed for sub-requests (using the NS option), e.g.

Code:
RewriteCond "%{ENV:lorem}#%{ENV:foo}" "(.+)#.*(bar).*" [NC]
RewriteRule ".*" - [E=lorem:%1%2,NE,NS]

This RewriteCond will only update lorem if it's currently not null. If you need the null case too, change the RHS (.+) to (.*)

I used "LogLevel rewrite:trace5" to see what mod_rewrite is doing.

Hope this helps get you further.
Back to top
csdude55



Joined: 22 Jan 2023
Posts: 17
Location: USA, Wilkesboro

PostPosted: Wed 01 Feb '23 0:00    Post subject: Reply with quote

Thank so much, @tangent!! That worked perfectly Very Happy I was just RIIIIGHT there, too! LOL

I was originally using mod_rewrite for this, but SetEnvIf has become a much better solution. The issue there, of course, is the order of operations. I don't THINK that I'll have an issue with subrequests, but thanks for the tip! I'll do some heavy testing before making it live.
Back to top


Reply to topic   Topic: Appending or joining ENV with SetEnv or SetEnvIf View previous topic :: View next topic
Post new topic   Forum Index -> Apache