2015-11-01 01:22:41 +08:00
|
|
|
/**
|
|
|
|
|
* Address.h
|
|
|
|
|
*
|
|
|
|
|
* An AMQP address in the "amqp://user:password@hostname:port/vhost" notation
|
|
|
|
|
*
|
|
|
|
|
* @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
|
|
|
|
|
* @copyright 2015 Copernica BV
|
|
|
|
|
*/
|
2018-03-02 19:14:43 +08:00
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
/**
|
|
|
|
|
* Include guard
|
|
|
|
|
*/
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set up namespace
|
|
|
|
|
*/
|
|
|
|
|
namespace AMQP {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class definition
|
|
|
|
|
*/
|
|
|
|
|
class Address
|
|
|
|
|
{
|
|
|
|
|
private:
|
2018-03-02 19:14:43 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The auth method
|
|
|
|
|
* @var std::string
|
|
|
|
|
*/
|
|
|
|
|
std::string _authmethod;
|
|
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
/**
|
|
|
|
|
* Login data (username + password)
|
|
|
|
|
* @var Login
|
|
|
|
|
*/
|
|
|
|
|
Login _login;
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
/**
|
|
|
|
|
* The hostname
|
|
|
|
|
* @var std::string
|
|
|
|
|
*/
|
|
|
|
|
std::string _hostname;
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
/**
|
|
|
|
|
* Port number
|
|
|
|
|
* @var uint16_t
|
|
|
|
|
*/
|
|
|
|
|
uint16_t _port = 5672;
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
/**
|
|
|
|
|
* The vhost
|
|
|
|
|
* @var std::string
|
|
|
|
|
*/
|
|
|
|
|
std::string _vhost;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Constructor to parse an address string
|
2017-05-02 03:59:45 +08:00
|
|
|
* The address should start with "amqp://
|
|
|
|
|
* @param data
|
|
|
|
|
* @param size
|
2015-11-01 01:22:41 +08:00
|
|
|
* @throws std::runtime_error
|
|
|
|
|
*/
|
2017-05-02 03:59:45 +08:00
|
|
|
Address(const char *data, size_t size) : _vhost("/")
|
2015-11-01 01:22:41 +08:00
|
|
|
{
|
2017-05-02 03:59:45 +08:00
|
|
|
// position of the last byte
|
|
|
|
|
const char *last = data + size;
|
|
|
|
|
|
2018-03-02 19:14:43 +08:00
|
|
|
// must start with amqp:// or ampqs://
|
|
|
|
|
if (strncmp(data, "amqps://", 8) == 0) _authmethod = std::string("amqps://");
|
|
|
|
|
else if (strncmp(data, "amqp://", 7) == 0) _authmethod = std::string("amqp://");
|
|
|
|
|
else throw std::runtime_error("AMQP address should start with \"amqp://\" or \"amqps://\"");
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2017-05-02 03:59:45 +08:00
|
|
|
// begin of the string was parsed
|
2018-03-02 19:54:09 +08:00
|
|
|
data += _authmethod.length();
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
// do we have a '@' to split user-data and hostname?
|
2017-05-02 03:59:45 +08:00
|
|
|
const char *at = (const char *)memchr(data, '@', last - data);
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
// do we have one?
|
|
|
|
|
if (at != nullptr)
|
|
|
|
|
{
|
2017-05-02 03:59:45 +08:00
|
|
|
// size of the user:password
|
|
|
|
|
size_t loginsize = at - data;
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
// colon could split username and password
|
2017-05-02 03:59:45 +08:00
|
|
|
const char *colon = (const char *)memchr(data, ':', loginsize);
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
// assign the login
|
|
|
|
|
_login = Login(
|
2017-05-02 03:59:45 +08:00
|
|
|
std::string(data, colon ? colon - data : loginsize),
|
2015-11-01 01:22:41 +08:00
|
|
|
std::string(colon ? colon + 1 : "", colon ? at - colon - 1 : 0)
|
|
|
|
|
);
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2017-05-02 03:59:45 +08:00
|
|
|
// set data to the start of the hostname
|
|
|
|
|
data = at + 1;
|
2015-11-01 01:22:41 +08:00
|
|
|
}
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
// find out where the vhost is set (starts with a slash)
|
2017-05-02 03:59:45 +08:00
|
|
|
const char *slash = (const char *)memchr(data, '/', last - data);
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
// was a vhost set?
|
2017-05-11 19:59:07 +08:00
|
|
|
if (slash != nullptr && last - slash > 1) _vhost.assign(slash + 1, last - slash - 1);
|
|
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
// the hostname is everything until the slash, check is portnumber was set
|
2017-05-02 03:59:45 +08:00
|
|
|
const char *colon = (const char *)memchr(data, ':', last - data);
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
// was a portnumber specified (colon must appear before the slash of the vhost)
|
|
|
|
|
if (colon && (!slash || colon < slash))
|
|
|
|
|
{
|
|
|
|
|
// a portnumber was set to
|
2017-05-02 03:59:45 +08:00
|
|
|
_hostname.assign(data, colon - data);
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2017-05-02 03:59:45 +08:00
|
|
|
// calculate the port
|
|
|
|
|
_port = atoi(std::string(colon + 1, slash ? slash - colon - 1 : last - colon - 1).data());
|
2015-11-01 01:22:41 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// no portnumber was set
|
2017-05-02 03:59:45 +08:00
|
|
|
_hostname.assign(data, slash ? slash - data : last - data);
|
2015-11-01 01:22:41 +08:00
|
|
|
}
|
|
|
|
|
}
|
2017-05-02 03:59:45 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor to parse an address string
|
2018-03-02 19:14:43 +08:00
|
|
|
* The address should start with amqp:// or amqps://
|
2017-05-02 03:59:45 +08:00
|
|
|
* @param data
|
|
|
|
|
* @throws std::runtime_error
|
|
|
|
|
*/
|
|
|
|
|
Address(const char *data) : Address(data, strlen(data)) {}
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2015-11-01 19:17:33 +08:00
|
|
|
/**
|
|
|
|
|
* Constructor based on std::string
|
|
|
|
|
* @param address
|
|
|
|
|
*/
|
2017-05-02 03:59:45 +08:00
|
|
|
Address(const std::string &address) : Address(address.data(), address.size()) {}
|
2015-11-03 00:47:21 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor based on already known properties
|
2018-03-02 19:14:43 +08:00
|
|
|
* @param authmethod
|
2015-11-03 00:47:21 +08:00
|
|
|
* @param host
|
|
|
|
|
* @param port
|
|
|
|
|
* @param login
|
|
|
|
|
* @param vhost
|
|
|
|
|
*/
|
2018-03-02 19:14:43 +08:00
|
|
|
Address(std::string authmethod, std::string host, uint16_t port, Login login, std::string vhost) :
|
|
|
|
|
_authmethod(std::move(authmethod)),
|
2017-05-02 03:59:45 +08:00
|
|
|
_login(std::move(login)),
|
|
|
|
|
_hostname(std::move(host)),
|
2017-05-11 19:59:07 +08:00
|
|
|
_port(port),
|
2017-05-02 03:59:45 +08:00
|
|
|
_vhost(std::move(vhost)) {}
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
/**
|
|
|
|
|
* Destructor
|
|
|
|
|
*/
|
2017-05-02 03:59:45 +08:00
|
|
|
virtual ~Address() = default;
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2018-03-02 19:14:43 +08:00
|
|
|
/**
|
|
|
|
|
* Expose the login data
|
|
|
|
|
* @return Login
|
|
|
|
|
*/
|
|
|
|
|
const std::string &authmethod() const
|
|
|
|
|
{
|
|
|
|
|
return _authmethod;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
/**
|
|
|
|
|
* Expose the login data
|
|
|
|
|
* @return Login
|
|
|
|
|
*/
|
|
|
|
|
const Login &login() const
|
|
|
|
|
{
|
|
|
|
|
return _login;
|
|
|
|
|
}
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
/**
|
|
|
|
|
* Host name
|
|
|
|
|
* @return std::string
|
|
|
|
|
*/
|
|
|
|
|
const std::string &hostname() const
|
|
|
|
|
{
|
|
|
|
|
return _hostname;
|
|
|
|
|
}
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
/**
|
|
|
|
|
* Port number
|
|
|
|
|
* @return uint16_t
|
|
|
|
|
*/
|
|
|
|
|
uint16_t port() const
|
|
|
|
|
{
|
|
|
|
|
return _port;
|
|
|
|
|
}
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
/**
|
|
|
|
|
* The vhost to connect to
|
|
|
|
|
* @return std::string
|
|
|
|
|
*/
|
|
|
|
|
const std::string &vhost() const
|
|
|
|
|
{
|
|
|
|
|
return _vhost;
|
|
|
|
|
}
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
/**
|
|
|
|
|
* Cast to a string
|
2018-03-02 19:14:43 +08:00
|
|
|
* @return std::string
|
2015-11-01 01:22:41 +08:00
|
|
|
*/
|
|
|
|
|
operator std::string () const
|
|
|
|
|
{
|
|
|
|
|
// result object
|
2018-03-02 19:14:43 +08:00
|
|
|
std::string str(_authmethod);
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
// append login
|
2015-11-03 00:53:43 +08:00
|
|
|
str.append(_login.user()).append(":").append(_login.password()).append("@").append(_hostname);
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
// do we need a special portnumber?
|
|
|
|
|
if (_port != 5672) str.append(":").append(std::to_string(_port));
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
// append default vhost
|
|
|
|
|
str.append("/");
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
// do we have a special vhost?
|
|
|
|
|
if (_vhost != "/") str.append(_vhost);
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2015-11-01 01:22:41 +08:00
|
|
|
// done
|
|
|
|
|
return str;
|
|
|
|
|
}
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2017-04-26 15:00:01 +08:00
|
|
|
/**
|
|
|
|
|
* Comparison operator
|
|
|
|
|
* @param that
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
bool operator==(const Address &that) const
|
|
|
|
|
{
|
2018-03-02 19:14:43 +08:00
|
|
|
// auth method should match
|
|
|
|
|
if (_authmethod != that._authmethod) return false;
|
|
|
|
|
|
2017-04-26 15:00:01 +08:00
|
|
|
// logins must match
|
|
|
|
|
if (_login != that._login) return false;
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2017-12-30 02:27:40 +08:00
|
|
|
// hostname must match, but are not case sensitive
|
2017-04-26 15:00:01 +08:00
|
|
|
if (strcasecmp(_hostname.data(), that._hostname.data()) != 0) return false;
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2017-04-26 15:00:01 +08:00
|
|
|
// portnumber must match
|
|
|
|
|
if (_port != that._port) return false;
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2017-04-26 15:00:01 +08:00
|
|
|
// and finally the vhosts, they must match too
|
|
|
|
|
return _vhost == that._vhost;
|
|
|
|
|
}
|
2017-05-11 19:59:07 +08:00
|
|
|
|
2017-04-26 15:00:01 +08:00
|
|
|
/**
|
|
|
|
|
* Comparison operator
|
|
|
|
|
* @param that
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
bool operator!=(const Address &that) const
|
|
|
|
|
{
|
|
|
|
|
// the opposite of operator==
|
|
|
|
|
return !operator==(that);
|
|
|
|
|
}
|
2017-06-21 15:44:52 +08:00
|
|
|
|
2017-12-30 02:27:40 +08:00
|
|
|
/**
|
|
|
|
|
* Comparison operator that is useful if addresses have to be ordered
|
|
|
|
|
* @param that
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
bool operator<(const Address &that) const
|
|
|
|
|
{
|
2018-03-02 19:14:43 +08:00
|
|
|
// compare auth methods
|
|
|
|
|
if (_authmethod != that._authmethod) return _authmethod < that._authmethod;
|
|
|
|
|
|
2017-12-30 02:27:40 +08:00
|
|
|
// compare logins
|
|
|
|
|
if (_login != that._login) return _login < that._login;
|
|
|
|
|
|
|
|
|
|
// hostname must match, but are not case sensitive
|
|
|
|
|
int result = strcasecmp(_hostname.data(), that._hostname.data());
|
|
|
|
|
|
|
|
|
|
// if hostnames are not equal, we know the result
|
|
|
|
|
if (result != 0) return result < 0;
|
|
|
|
|
|
|
|
|
|
// portnumber must match
|
|
|
|
|
if (_port != that._port) return _port < that._port;
|
|
|
|
|
|
|
|
|
|
// and finally compare the vhosts
|
|
|
|
|
return _vhost < that._vhost;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-21 15:44:52 +08:00
|
|
|
/**
|
|
|
|
|
* Friend function to allow writing the address to a stream
|
|
|
|
|
* @param stream
|
|
|
|
|
* @param address
|
|
|
|
|
* @return std::ostream
|
|
|
|
|
*/
|
|
|
|
|
friend std::ostream &operator<<(std::ostream &stream, const Address &address)
|
|
|
|
|
{
|
|
|
|
|
// start with the protocol and login
|
2018-03-02 19:14:43 +08:00
|
|
|
stream << address._authmethod << address._login;
|
2017-06-21 15:44:52 +08:00
|
|
|
|
|
|
|
|
// do we need a special portnumber?
|
|
|
|
|
if (address._port != 5672) stream << ":" << address._port;
|
|
|
|
|
|
|
|
|
|
// append default vhost
|
|
|
|
|
stream << "/";
|
|
|
|
|
|
|
|
|
|
// do we have a special vhost?
|
|
|
|
|
if (address._vhost != "/") stream << address._vhost;
|
|
|
|
|
|
|
|
|
|
// done
|
|
|
|
|
return stream;
|
|
|
|
|
}
|
2015-11-01 01:22:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* End of namespace
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|