-
Notifications
You must be signed in to change notification settings - Fork 6
(Proposal) RedisAsIs
This new mechanism would add support to return ad-hoc redis content in response to HTTP requests. This would differ from current mod_redis in that no formatting would be applied on the redis value at all. However, it would be possible for HTTP response headers to be set against each piece of content and a default set of global response headers.
Description: Define a rule to match a URL to a Redis value and optional hash for response headers Syntax: RedisAsIs TestString option ...
TestString
A regular expression defining the elements in the URL to match, in the same manner as the RedisAlias directive. The only difference in this case is that there must be only one backreference which must be the name of the Redis key storing the content to be sent in response to a GET.
For example,
^static/([^/]+)$
would mean that the URL http://localhost/redis/static/index.html
would map on to a redis key index.html
Option can be one of
contentKey=regexp Defines the key holding the body of the HTTP response. If not defined then the backreference group from the test string is used.
If this value is defined then it can hold one token $1
, which will be replaced with the backreference from the test string. For example, the regexp website1-$1
would generate the key website1-index.html
with the above example.
responseHeaders=regexp Defines the key holding the hash which defines the HTTP response headers. If not defined defaults to the same value as the content with the suffix .headers
, e.g. in the example above the default headers would be index.html.headers
If this value is defined that it can hold one token $1
, which will be replaced with the backreference from the test string. For example, the regexp response-headers-$1\\.hash
would generate the key response-headers-index.html.hash
with the above example.
defaultResponseHeaders=key Defines the key holding the hash which defines the default HTTP response headers. If not defined no default value is used. See below for details of how response headers are merged.
default404Content=regexp Defines a key holding a value that can be returned as the body when a 404 response is to be returned. If defined then the content can hold the token $1 which would be replaced with the path that was not found. For example, the URL http://localhost:8080/no/such/page.html
would replace the token with /no/such/page.html
This may be useful for JSONP style Ajax calls where notification of 404 response may need to be made via a javascript callback.
Response headers are stored as Redis hash types. When only one of either the defaultResponseHeaders
or responseHeaders
is found then they are simply returned verbatim. However, when both are present then the two are merged, with values in responseHeaders
overwriting equivalents in defaultResponseHeaders
.
For example, given a setup such as :
redis 127.0.0.1:6379> HGETALL defaultResponseHeaders
1) "Content-Type"
2) "text/html; charset=utf-8"
3) "X-Powered-By"
4) "mod_redis"
redis 127.0.0.1:6379> HGETALL responseHeaders
1) "Content-Type"
2) "text/json"
Then the resulting response headers would be
Content-Type: text/json
X-Powered-By: mod_redis
When requesting a URL that does not resolve to a redis key (that is, redis returns nil for the key) then a 404 response will be returned, along with the default 404 body content where defined.
When requesting a URL that resolves to a redis value that is anything other than a String (e.g. a hash, set, sorted set, list etc) then a 406 (Not acceptable) response will be returned.
This behaviour to be confirmed. If implemented then if the responseHeader holds an ETag value then subsequent requests with the If-None-Match request header would behave as expected.
With this Apache configuration
<Location ~ /redis/*>
SetHandler redis
</Location>
RedisAsIs ^static/([^/]+)$ contentKey=static_$1 responseHeaders=response_$1 defaultResponseHeaders=default_responses
and with this redis setup
redis 127.0.0.1:6379> GET static_index.txt
"Hello world !"
redis 127.0.0.1:6379> HGETALL responseHeaders
1) "Content-Type"
2) "text/plain; charset=utf-8"
redis 127.0.0.1:6379> HGETALL defaultResponseHeaders
1) "Content-Type"
2) "text/html; charset=utf-8"
3) "X-Powered-By"
4) "mod_redis"
then a request to http://localhost/redis/static/index.txt
would return
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
X-Powered-By: mod_redis
Hello World !