Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support full Redis IANA URI scheme #1842

Merged
merged 2 commits into from
Apr 2, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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