Skip to content

Commit

Permalink
Merge pull request #1842 from wilzbach/redis-iana
Browse files Browse the repository at this point in the history
Support full Redis IANA URI scheme. Fixes #1896.
  • Loading branch information
s-ludwig authored Apr 2, 2018
2 parents 892e094 + 81f5fc9 commit d4df514
Showing 1 changed file with 66 additions and 3 deletions.
69 changes: 66 additions & 3 deletions redis/vibe/db/redis/redis.d
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,78 @@ RedisClient connectRedis(string host, ushort port = RedisClient.defaultPort)
}

/**
Returns a Redis database connecction instance corresponding to the given URL.
Returns a Redis database connection instance corresponding to the given URL.
The URL must be of the format "redis://server[:port]/dbnum".
Authentication:
Authenticated connections are supported by using a URL connection string
such as "redis://password@host".
Examples:
---
// connecting with default settings:
auto redisDB = connectRedisDB("redis://127.0.0.1");
---
---
// connecting using the URL form with custom settings
auto redisDB = connectRedisDB("redis://password:myremotehost/3?maxmemory=10000000");
---
Params:
url = Redis URI scheme for a Redis database instance
host_or_url = Can either be a host name, in which case the default port will be used, or a URL with the redis:// scheme.
Returns:
A new RedisDatabase instance that can be used to access the database.
See_also: $(LINK2 https://www.iana.org/assignments/uri-schemes/prov/redis, Redis URI scheme)
*/
RedisDatabase connectRedisDB(URL url)
{
auto cli = connectRedis(url.host, url.port != 0 ? url.port : RedisClient.defaultPort);
// TODO: support password
return cli.getDatabase(url.localURI[1 .. $].to!long);

if (!url.queryString.empty)
{
import vibe.inet.webform : FormFields, parseURLEncodedForm;
auto query = FormFields.init;
parseURLEncodedForm(url.queryString, query);
foreach (param, val; query.byKeyValue)
{
switch (param)
{
/**
The password to use for the Redis AUTH command comes from either the
password portion of the "userinfo" URI field or the value from the
key-value pair from the "query" URI field with the key "password".
If both the password portion of the "userinfo" URI field and a
"query" URI field key-value pair with the key "password" are present,
the semantics for what password to use for authentication are not
well-defined. Such situations therefore ought to be avoided.
*/
case "password":
if (!url.password.empty)
cli.auth(val);
break;
default:
throw new Exception(`Redis config parameter "` ~ param ~ `" isn't supported`);
}
}
}

/*
Redis' current optional authentication mechanism does not employ a
username, but this might change in the future
*/
if (!url.password.empty)
cli.auth(url.password);

long databaseIndex;
if (url.localURI.length >= 2)
databaseIndex = url.pathString[1 .. $].to!long;

return cli.getDatabase(databaseIndex);
}

/**
Expand Down

0 comments on commit d4df514

Please sign in to comment.