Merge pull request #1234 from vighnesh-sawant/standard-port-enhancment

Add functions which return displayable addresses
This commit is contained in:
Kelson
2025-10-31 14:55:35 +01:00
committed by GitHub
3 changed files with 45 additions and 20 deletions

View File

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

View File

@@ -29,6 +29,22 @@
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) :
mp_library(library),
mp_nameMapper(nameMapper),
@@ -56,7 +72,13 @@ bool Server::start() {
m_ipConnectionLimit,
m_catalogOnlyMode,
m_contentServerUrl));
return mp_server->start();
if (mp_server->start()) {
// this syncs m_addr of InternalServer and Server as they may diverge
m_addr = mp_server->getAddress();
return true;
} else {
return false;
}
}
void Server::stop() {
@@ -69,12 +91,12 @@ void Server::stop() {
void Server::setRoot(const std::string& root)
{
m_root = root;
if (m_root[0] != '/') {
m_root = "/" + m_root;
}
if (m_root.back() == '/') {
m_root.erase(m_root.size() - 1);
}
while (!m_root.empty() && m_root.back() == '/')
m_root.pop_back();
while (!m_root.empty() && m_root.front() == '/')
m_root = m_root.substr(1);
m_root = m_root.empty() ? m_root : "/" + m_root;
}
void Server::setAddress(const std::string& addr)
@@ -93,12 +115,12 @@ void Server::setAddress(const std::string& addr)
int Server::getPort() const
{
return mp_server->getPort();
return m_port;
}
IpAddress Server::getAddress() const
{
return mp_server->getAddress();
return m_addr;
}
IpMode Server::getIpMode() const
@@ -106,4 +128,16 @@ IpMode Server::getIpMode() const
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;
}
}

View File

@@ -99,16 +99,6 @@ bool ipAvailable(const std::string addr)
return false;
}
inline std::string normalizeRootUrl(std::string rootUrl)
{
while ( !rootUrl.empty() && rootUrl.back() == '/' )
rootUrl.pop_back();
while ( !rootUrl.empty() && rootUrl.front() == '/' )
rootUrl = rootUrl.substr(1);
return rootUrl.empty() ? rootUrl : "/" + rootUrl;
}
std::string
fullURL2LocalURL(const std::string& fullUrl, const std::string& rootLocation)
{
@@ -440,7 +430,7 @@ InternalServer::InternalServer(LibraryPtr library,
std::string contentServerUrl) :
m_addr(addr),
m_port(port),
m_root(normalizeRootUrl(root)),
m_root(root),
m_rootPrefixOfDecodedURL(m_root),
m_nbThreads(nbThreads),
m_multizimSearchLimit(multizimSearchLimit),