Implement a function which returns server access url

This commit is contained in:
Vighnesh
2025-10-26 20:56:36 +05:30
parent c2df0a99fe
commit 4928509991
2 changed files with 29 additions and 0 deletions

View File

@@ -69,6 +69,7 @@ namespace kiwix
int getPort() const; int getPort() const;
IpAddress getAddress() const; IpAddress getAddress() const;
IpMode getIpMode() const; IpMode getIpMode() const;
std::vector<std::string> getServerAccessUrls() const;
protected: protected:
std::shared_ptr<Library> mp_library; std::shared_ptr<Library> mp_library;

View File

@@ -29,6 +29,22 @@
namespace kiwix { namespace kiwix {
namespace
{
std::string makeServerUrl(std::string host, int port, std::string root)
{
const int httpDefaultPort = 80;
if (port == httpDefaultPort) {
return "http://" + host + root;
} else {
return "http://" + host + ":" + std::to_string(port) + root;
}
}
} // unnamed namespace
Server::Server(LibraryPtr library, std::shared_ptr<NameMapper> nameMapper) : Server::Server(LibraryPtr library, std::shared_ptr<NameMapper> nameMapper) :
mp_library(library), mp_library(library),
mp_nameMapper(nameMapper), mp_nameMapper(nameMapper),
@@ -112,4 +128,16 @@ IpMode Server::getIpMode() const
return mp_server->getIpMode(); return mp_server->getIpMode();
} }
std::vector<std::string> Server::getServerAccessUrls() const
{
std::vector<std::string> result;
if (!m_addr.addr.empty()) {
result.push_back(makeServerUrl(m_addr.addr, m_port, m_root));
}
if (!m_addr.addr6.empty()) {
result.push_back(makeServerUrl("[" + m_addr.addr6 + "]", m_port, m_root));
}
return result;
}
} }