Skip to content

Commit 200bf7a

Browse files
Implement a function which returns server access url
1 parent dd09e3c commit 200bf7a

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

include/server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ namespace kiwix
6969
int getPort() const;
7070
IpAddress getAddress() const;
7171
IpMode getIpMode() const;
72+
std::vector<std::string> getServerAccessUrls() const;
7273

7374
protected:
7475
std::shared_ptr<Library> mp_library;

src/server.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@
2929

3030
namespace kiwix {
3131

32+
namespace
33+
{
34+
35+
std::string makeServerUrl(std::string host, int port, std::string root)
36+
{
37+
const int httpDefaultPort = 80;
38+
39+
if (port == httpDefaultPort) {
40+
return "http://" + host + root;
41+
} else {
42+
return "http://" + host + ":" + std::to_string(port) + root;
43+
}
44+
}
45+
46+
} // unnamed namespace
47+
3248
Server::Server(LibraryPtr library, std::shared_ptr<NameMapper> nameMapper) :
3349
mp_library(library),
3450
mp_nameMapper(nameMapper),
@@ -106,4 +122,19 @@ IpMode Server::getIpMode() const
106122
return mp_server->getIpMode();
107123
}
108124

125+
std::vector<std::string> Server::getServerAccessUrls() const
126+
{
127+
std::vector<std::string> result;
128+
const kiwix::IpAddress addresses = Server::getAddress();
129+
const int port = Server::getPort();
130+
const std::string root = mp_server->getRootPrefixOfDecodedURL();
131+
if (!addresses.addr.empty()) {
132+
result.push_back(makeServerUrl(addresses.addr, port, root));
133+
}
134+
if (!addresses.addr6.empty()) {
135+
result.push_back(makeServerUrl("[" + addresses.addr6 + "]", port, root));
136+
}
137+
return result;
109138
}
139+
140+
}

src/server/internalServer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ class InternalServer {
124124
IpAddress getAddress() const { return m_addr; }
125125
int getPort() const { return m_port; }
126126
IpMode getIpMode() const { return m_ipMode; }
127+
std::string getRootPrefixOfDecodedURL() const { return m_rootPrefixOfDecodedURL; }
127128

128129
private: // functions
129130
std::unique_ptr<Response> handle_request(const RequestContext& request);

0 commit comments

Comments
 (0)