2929
3030namespace 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+
3248Server::Server (LibraryPtr library, std::shared_ptr<NameMapper> nameMapper) :
3349 mp_library (library),
3450 mp_nameMapper (nameMapper),
@@ -56,7 +72,13 @@ bool Server::start() {
5672 m_ipConnectionLimit,
5773 m_catalogOnlyMode,
5874 m_contentServerUrl));
59- return mp_server->start ();
75+ if (mp_server->start ()) {
76+ // this syncs m_addr of InternalServer and Server as they may diverge
77+ m_addr = mp_server->getAddress ();
78+ return true ;
79+ } else {
80+ return false ;
81+ }
6082}
6183
6284void Server::stop () {
@@ -69,12 +91,12 @@ void Server::stop() {
6991void Server::setRoot (const std::string& root)
7092{
7193 m_root = root;
72- if ( m_root[ 0 ] != ' /' ) {
73- m_root = " / " + m_root ;
74- }
75- if ( m_root.back () == ' /' ) {
76- m_root. erase ( m_root.size () - 1 );
77- }
94+ while (! m_root. empty () && m_root. back () == ' /' )
95+ m_root. pop_back () ;
96+
97+ while (! m_root.empty () && m_root. front () == ' /' )
98+ m_root = m_root.substr ( 1 );
99+ m_root = m_root. empty () ? m_root : " / " + m_root;
78100}
79101
80102void Server::setAddress (const std::string& addr)
@@ -93,17 +115,29 @@ void Server::setAddress(const std::string& addr)
93115
94116int Server::getPort () const
95117{
96- return mp_server-> getPort () ;
118+ return m_port ;
97119}
98120
99121IpAddress Server::getAddress () const
100122{
101- return mp_server-> getAddress () ;
123+ return m_addr ;
102124}
103125
104126IpMode Server::getIpMode () const
105127{
106128 return mp_server->getIpMode ();
107129}
108130
131+ std::vector<std::string> Server::getServerAccessUrls () const
132+ {
133+ std::vector<std::string> result;
134+ if (!m_addr.addr .empty ()) {
135+ result.push_back (makeServerUrl (m_addr.addr , m_port, m_root));
136+ }
137+ if (!m_addr.addr6 .empty ()) {
138+ result.push_back (makeServerUrl (" [" + m_addr.addr6 + " ]" , m_port, m_root));
139+ }
140+ return result;
141+ }
142+
109143}
0 commit comments