Add user:pass support for URI

This commit is contained in:
badaix 2025-02-05 23:01:04 +01:00 committed by Johannes Pohl
parent a72c6948b1
commit 42651476f1
3 changed files with 31 additions and 0 deletions

View file

@ -74,6 +74,15 @@ void StreamUri::parse(const std::string& stream_uri)
tmp = tmp.substr(2); tmp = tmp.substr(2);
// tmp = [user:password@]host[:port][/]path[?query][#fragment] // tmp = [user:password@]host[:port][/]path[?query][#fragment]
pos = tmp.find('@');
if (pos != std::string::npos)
{
user = tmp.substr(0, pos);
if (user.find(':') != std::string::npos)
user = strutils::split_left(user, ':', password);
tmp = tmp.substr(pos + 1);
}
pos = tmp.find('/'); pos = tmp.find('/');
if (pos == string::npos) if (pos == string::npos)
{ {

View file

@ -43,6 +43,12 @@ struct StreamUri
std::string uri; std::string uri;
/// the scheme component (pipe, http, file, tcp, ...) /// the scheme component (pipe, http, file, tcp, ...)
std::string scheme; std::string scheme;
/// user name
std::string user;
/// password
std::string password;
// struct Authority // struct Authority
// { // {
// std::string username; // std::string username;

View file

@ -225,6 +225,22 @@ TEST_CASE("Uri")
REQUIRE(uri.path == "/tmp/snapfifo"); REQUIRE(uri.path == "/tmp/snapfifo");
REQUIRE(uri.host.empty()); REQUIRE(uri.host.empty());
uri = StreamUri("wss://user:pass@localhost:1788");
REQUIRE(uri.scheme == "wss");
REQUIRE(uri.path.empty());
REQUIRE(uri.host == "localhost");
REQUIRE(uri.user == "user");
REQUIRE(uri.password == "pass");
REQUIRE(uri.port == 1788);
uri = StreamUri("wss://user@localhost:1788");
REQUIRE(uri.scheme == "wss");
REQUIRE(uri.path.empty());
REQUIRE(uri.host == "localhost");
REQUIRE(uri.user == "user");
REQUIRE(uri.password.empty());
REQUIRE(uri.port == 1788);
// uri = StreamUri("scheme:[//host[:port]][/]path[?query=none][#fragment]"); // uri = StreamUri("scheme:[//host[:port]][/]path[?query=none][#fragment]");
// Test with all fields // Test with all fields
uri = StreamUri("scheme://host:42/path?query=none&key=value#fragment"); uri = StreamUri("scheme://host:42/path?query=none&key=value#fragment");