Skip to content

Commit 36c5df8

Browse files
Add functions which return displayable addresses
1 parent dd09e3c commit 36c5df8

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

include/server.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ namespace kiwix
6969
int getPort() const;
7070
IpAddress getAddress() const;
7171
IpMode getIpMode() const;
72+
std::string getDisplayableAddress();
73+
std::string getDisplayableAddress6();
7274

7375
protected:
7476
std::shared_ptr<Library> mp_library;

src/server.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,43 @@ IpMode Server::getIpMode() const
106106
return mp_server->getIpMode();
107107
}
108108

109+
std::string Server::getDisplayableAddress()
110+
{
111+
kiwix::IpAddress addresses = Server::getAddress();
112+
if(addresses.addr.empty()) return getDisplayableAddress6(); //as a fallback return ipv6 address as one of them has to be set
113+
114+
std::string result = "http://";
115+
std::string suffix;
116+
117+
if(Server::getPort()==80)
118+
{
119+
suffix = mp_server->getRootPrefixOfDecodedURL();
120+
}else
121+
{
122+
suffix = ":" + std::to_string(Server::getPort()) + mp_server->getRootPrefixOfDecodedURL();
123+
}
124+
125+
result = result + addresses.addr + suffix;
126+
return result;
127+
}
128+
129+
std::string Server::getDisplayableAddress6()
130+
{
131+
kiwix::IpAddress addresses = Server::getAddress();
132+
if(addresses.addr6.empty()) return getDisplayableAddress(); //as a fallback return ipv4 address as one of them has to be set
133+
134+
std::string result = "http://";
135+
std::string suffix;
136+
137+
if(Server::getPort()==80)
138+
{
139+
suffix = mp_server->getRootPrefixOfDecodedURL();
140+
}else
141+
{
142+
suffix = ":" + std::to_string(Server::getPort()) + mp_server->getRootPrefixOfDecodedURL();
143+
}
144+
145+
result = result + "[" + addresses.addr6 + "]" + suffix;
146+
return result;
109147
}
148+
}

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)