7

I am trying to use SetEnvIf (Apache2, via a .htaccess file) to store an environment variable where the value to be assigned to the variable needs to contain spaces. For example, trying to set environment variable "AUTH_KEY" to have value "a b":

SetEnvIf Request_URI "^/example" AUTH_KEY="a\ b"

No matter what I do, I have not been able to escape the space in the value - the space acts as a delimiter between env vars to be created (in the above AUTH_KEY is created with value '"a\' and a second env var is created called 'b"'). I have tried single-quotes too with no luck.

How do I escape the space character?

HopelessN00b
  • 53,795
  • 33
  • 135
  • 209
Alex B
  • 141
  • 1
  • 5
  • This is a terrible shot in the dark and I don't even expect it to work. What happens if you URLencode the space? (i.e. use %25 in place of one) Yes, I know this isn't a URL and I don't expect it to be treated like one. – Andrew B Apr 04 '13 at 22:05
  • Thanks Andrew, in the end I figured out how to add spaces without having to use escaping (it works for me at least). I found that placing the quotes around the key=value, rather than just the value was the way to go. – Alex B Apr 05 '13 at 13:52

2 Answers2

7

In the end I figured it out for myself…

SetEnvIf Request_URI "^/example" "AUTH_KEY=a b"

… which looks odd, but works. It correctly sets AUTH_KEY to value a b

Hopefully this might save someone else's time in the future.

The details:- I am not a C programmer, but bumbling around on Google I found mod_setenvif.c listed at http://www.bvbcode.com/code/s6148jvr-385031

A function is called on line 405 (ap_getword_conf, for which I found detail at http://docstore.mik.ua/orelly/apache_mod/155.htm) which seems to parse strings, delimited by whitespace (but optionally encapsulated in quotes and accepting the use of escape characters).

I noticed that this happened before the sub-string returned by the function above being split by the '=' character (line 411, by function ap_getword). Thus quotes around the key=value pair, rather than just the value.

Alex B
  • 141
  • 1
  • 5
0

Try using instead SetEnvIf Request_URI "^/example" AUTH_KEY="a\sb". \s here represents the space.

Meriadoc Brandybuck
  • 1,330
  • 9
  • 11