fix new bug in parsing amqp:// address where the default vhost was incorrectly overwritten
This commit is contained in:
parent
8501f19c0b
commit
b2877434cf
|
|
@ -28,19 +28,19 @@ private:
|
|||
* @var Login
|
||||
*/
|
||||
Login _login;
|
||||
|
||||
|
||||
/**
|
||||
* The hostname
|
||||
* @var std::string
|
||||
*/
|
||||
std::string _hostname;
|
||||
|
||||
|
||||
/**
|
||||
* Port number
|
||||
* @var uint16_t
|
||||
*/
|
||||
uint16_t _port = 5672;
|
||||
|
||||
|
||||
/**
|
||||
* The vhost
|
||||
* @var std::string
|
||||
|
|
@ -62,47 +62,47 @@ public:
|
|||
|
||||
// must start with amqp://
|
||||
if (size < 7 || strncmp(data, "amqp://", 7) != 0) throw std::runtime_error("AMQP address should start with \"amqp://\"");
|
||||
|
||||
|
||||
// begin of the string was parsed
|
||||
data += 7;
|
||||
|
||||
|
||||
// do we have a '@' to split user-data and hostname?
|
||||
const char *at = (const char *)memchr(data, '@', last - data);
|
||||
|
||||
|
||||
// do we have one?
|
||||
if (at != nullptr)
|
||||
{
|
||||
// size of the user:password
|
||||
size_t loginsize = at - data;
|
||||
|
||||
|
||||
// colon could split username and password
|
||||
const char *colon = (const char *)memchr(data, ':', loginsize);
|
||||
|
||||
|
||||
// assign the login
|
||||
_login = Login(
|
||||
std::string(data, colon ? colon - data : loginsize),
|
||||
std::string(colon ? colon + 1 : "", colon ? at - colon - 1 : 0)
|
||||
);
|
||||
|
||||
|
||||
// set data to the start of the hostname
|
||||
data = at + 1;
|
||||
}
|
||||
|
||||
|
||||
// find out where the vhost is set (starts with a slash)
|
||||
const char *slash = (const char *)memchr(data, '/', last - data);
|
||||
|
||||
|
||||
// was a vhost set?
|
||||
if (slash != nullptr && last - data > 1) _vhost.assign(slash + 1, last - slash - 1);
|
||||
|
||||
if (slash != nullptr && last - slash > 1) _vhost.assign(slash + 1, last - slash - 1);
|
||||
|
||||
// the hostname is everything until the slash, check is portnumber was set
|
||||
const char *colon = (const char *)memchr(data, ':', last - data);
|
||||
|
||||
|
||||
// was a portnumber specified (colon must appear before the slash of the vhost)
|
||||
if (colon && (!slash || colon < slash))
|
||||
{
|
||||
// a portnumber was set to
|
||||
_hostname.assign(data, colon - data);
|
||||
|
||||
|
||||
// calculate the port
|
||||
_port = atoi(std::string(colon + 1, slash ? slash - colon - 1 : last - colon - 1).data());
|
||||
}
|
||||
|
|
@ -120,7 +120,7 @@ public:
|
|||
* @throws std::runtime_error
|
||||
*/
|
||||
Address(const char *data) : Address(data, strlen(data)) {}
|
||||
|
||||
|
||||
/**
|
||||
* Constructor based on std::string
|
||||
* @param address
|
||||
|
|
@ -134,17 +134,17 @@ public:
|
|||
* @param login
|
||||
* @param vhost
|
||||
*/
|
||||
Address(std::string host, uint16_t port, Login login, std::string vhost) :
|
||||
Address(std::string host, uint16_t port, Login login, std::string vhost) :
|
||||
_login(std::move(login)),
|
||||
_hostname(std::move(host)),
|
||||
_port(port),
|
||||
_port(port),
|
||||
_vhost(std::move(vhost)) {}
|
||||
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*/
|
||||
virtual ~Address() = default;
|
||||
|
||||
|
||||
/**
|
||||
* Expose the login data
|
||||
* @return Login
|
||||
|
|
@ -153,7 +153,7 @@ public:
|
|||
{
|
||||
return _login;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Host name
|
||||
* @return std::string
|
||||
|
|
@ -162,7 +162,7 @@ public:
|
|||
{
|
||||
return _hostname;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Port number
|
||||
* @return uint16_t
|
||||
|
|
@ -171,7 +171,7 @@ public:
|
|||
{
|
||||
return _port;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The vhost to connect to
|
||||
* @return std::string
|
||||
|
|
@ -180,7 +180,7 @@ public:
|
|||
{
|
||||
return _vhost;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Cast to a string
|
||||
* @return std:string
|
||||
|
|
@ -189,23 +189,23 @@ public:
|
|||
{
|
||||
// result object
|
||||
std::string str("amqp://");
|
||||
|
||||
|
||||
// append login
|
||||
str.append(_login.user()).append(":").append(_login.password()).append("@").append(_hostname);
|
||||
|
||||
|
||||
// do we need a special portnumber?
|
||||
if (_port != 5672) str.append(":").append(std::to_string(_port));
|
||||
|
||||
|
||||
// append default vhost
|
||||
str.append("/");
|
||||
|
||||
|
||||
// do we have a special vhost?
|
||||
if (_vhost != "/") str.append(_vhost);
|
||||
|
||||
|
||||
// done
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Comparison operator
|
||||
* @param that
|
||||
|
|
@ -215,17 +215,17 @@ public:
|
|||
{
|
||||
// logins must match
|
||||
if (_login != that._login) return false;
|
||||
|
||||
|
||||
// hostname must match, but are nt case sensitive
|
||||
if (strcasecmp(_hostname.data(), that._hostname.data()) != 0) return false;
|
||||
|
||||
|
||||
// portnumber must match
|
||||
if (_port != that._port) return false;
|
||||
|
||||
|
||||
// and finally the vhosts, they must match too
|
||||
return _vhost == that._vhost;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Comparison operator
|
||||
* @param that
|
||||
|
|
|
|||
Loading…
Reference in New Issue